python3+selenium如何正确驱动360浏览器打开网页

@selenium

python3+selenium打开chrome浏览器

首先说明:这是笔记,仅供参考
1、pip install selenium。
2、下载chrome浏览器exe程序,记得查看chrome版本号。
3、下载对应chrome内核版本chromedriver.exe(一定要对应版本号,不要会报错的)。
链接: chromedriver.exe下载
4、直接先写个测试代码,运行代码。

from selenium import webdriver
#这是不添加PATH环境下的使用方法
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = r"chrome.exe" #这里是chrome浏览路径 添加r为了正常使用\
drive = webdriver.Chrome(executable_path=r'你的chromedriver.exe的路径',chrome_options=chrome_options)
print('这里')
drive.get('https://www.baidu.com/')
print(drive.title)
drive.quit()  # 建议使用 quit,close不会关闭进程中的chromedriver.exe

python3+selenium使用360浏览器

这里小小的记录下,刚开始使用了一个内核版本为43.xxx的浏览器,结合对应的23.xx chromedriver.exe,死活不能添加网址上去,地址栏一直显示data;。找了很多办法,无解,最后还是自己傻了,下个最新的内核为86.xxx360浏览器不就行了嘛。傻了,傻了…

# -------------使用360浏览器, 无法正常输入网址?????, 有的说是 chromedriver 对应版本问题,找了,还是不起作用

""" 最终确定原因,下载360内核86版本,下载chromedriver.exe 86版本 能正常驱动360浏览器 """
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = r"D:\360Safe\360se6\Application\360se.exe"  # 这里是360安全浏览器的路径

d = webdriver.Chrome('chromedriver86.exe',chrome_options=chrome_options)
print('这里')
help(d.get)
d.get('http://www.baidu.com')
if('百度' in d.title):
    ele=d.find_element_by_class_name('s_ipt')#元素定位
    ele.send_keys("wo")#模拟手动输入
    ele.clear()
    ele.send_keys('学院')
    time.sleep(10)
else:
    print("失败")
d.quit()

360浏览器:打开F12开发者。如果当前是兼容模式,则弹出的是IE的调试工具,如果当前是极速模式,则弹出的是谷歌的调试工具。

python3+selenium使用IE浏览器

1、跟chrome使用方法类似,下载对应版本的IEDriverServer.exe

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

ws = webdriver.Ie("IEDriverServer.exe")
	ws.get('http://npm.taobao.org/mirrors/chromedriver/')  # 
	# ws.maximize_window()  #  窗口最大化
	ws.quit()

python3+selenium接管已经打开的浏览器继续操作

这个有意思了,我以为已经打开的浏览器,能通过selenium监听到,直接接管,然而并不是我想的那样

# '先运行 chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"'

import os
a = os.system(r'chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"')

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#   ---------- 接管 chrome 浏览器,前提是 必须使用os.system 中的代码执行,并绑定端口9222 才能使用,目前360调试失败
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "./chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
driver.get('https://www.baidu.com/s?cl=3&tn=baidutop10&fr=top1000&wd=%E4%B8%AD%E4%BD%BF%E9%A6%86%E6%8F%90%E9%86%92%E5%9C%A8%E7%BE%8E%E4%B8%AD%E5%9B%BD%E5%85%AC%E6%B0%91%E5%8A%A0%E5%BC%BA%E9%98%B2%E8%8C%83&rsv_idx=2&rsv_dl=fyb_n_homepage&hisfilter=1')
print(driver.title)

说白了就是你要先启动一个9222端口的chrome程序,然后运行代码就能监听到,what?,那我要你干啥用,还要自己敲个代码启动一个,肯定还有其它办,是自己学术浅薄了,而且这个对360浏览器不起作用,没想到吧。

小白的爬虫之路啊…

怎么获取JavaScript加载出来的会话框呢?不是alert窗口,是需要填写内容的那种窗口。

解决了: 首先查看弹出窗口的源代码,是alert,还是JavaScript。通常js弹出的都是新窗口,而不是弹窗。所以需要捕获所有窗口,然后切换窗口才能继续操作。

allhandles2 = ws.window_handles  # 捕获所有窗口
ws.switch_to.window(allhandles2[1])  # 切换到窗口
# 切换窗口后就能继续进行操作了
ws.find_element_by_class_name('name').click()

Selenium 之 WebDriverWait 使用方法

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Selenium是一个广泛用于自动化Web浏览器测试的工具,但要让它支持360浏览器的兼容模式,你需要确保使用了正确浏览器驱动和设置。以下是使用Selenium Python与360浏览器兼容模式配合的步骤: 1. **安装360浏览器驱动**:首先,你需要下载360浏览器对应的ChromeDriver(因为360浏览器基于Chromium内核)。你可以从Selenium官方网站的`Drivers`部分找到适合360浏览器的版本:<https://sites.google.com/a/chromium.org/chromedriver/downloads> 2. **配置环境变量**:将下载好的ChromeDriver放到系统路径中的某个目录,或者设置一个环境变量`webdriver.chrome.driver`指向该驱动文件的路径。 3. **启动浏览器并选择模式**:在Python代码中,使用`DesiredCapabilities`类来指定浏览器类型和模式。例如,创建一个`DesiredCapabilities`对象,然后设置`chromeOptions`参数为一个包含`prefs`键的字典,这样可以模拟兼容模式: ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options # 创建一个新的ChromeOptions对象 chrome_options = Options() # 设置浏览器为兼容模式,这里使用的是一个临时的偏好设置策略 prefs = {"profile.default_content_settings.popups": 0, "profile.default_content_setting_values.media_stream_mic": 2, "profile.default_content_setting_values.media_stream_camera": 2, "profile.content_settings.exceptions.plugins.enabled": [{"id": "Chrome PDF Viewer", "setting": 1}]} chrome_options.add_experimental_option("prefs", prefs) # 创建一个DesiredCapabilities对象,指定为Chrome浏览器和兼容模式 desired_capabilities = DesiredCapabilities.CHROME.copy() desired_capabilities["browserName"] = "chrome" desired_capabilities["chromeOptions"] = chrome_options.to_capabilities() # 创建WebDriver实例,使用上述设置 driver = webdriver.Chrome(desired_capabilities=desired_capabilities) ``` 4. **打开页面**:现在你可以使用`driver.get()`方法加载网页了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值