python3+selenium如何正确驱动360浏览器打开网页
发布日期:2022-02-23 07:42:36 浏览次数:13 分类:技术文章

本文共 3166 字,大约阅读时间需要 10 分钟。

@

python3+selenium打开chrome浏览器

首先说明:这是笔记,仅供参考

1、pip install selenium。
2、下载chrome浏览器exe程序,记得查看chrome版本号。
3、下载对应chrome内核版本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 Optionsfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport timechrome_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 Optionsfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport timews = 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 osa = os.system(r'chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"')from selenium import webdriverfrom 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()

转载地址:https://blog.csdn.net/weixin_42781055/article/details/115308420 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Python3操作sqlmap
下一篇:python爬虫框架feapder的使用简介

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月29日 17时28分14秒