selenium+python自动化87-Chrome浏览器或火狐或其他浏览器后台启动模式(headless)

本文详细介绍了Selenium自动化测试工具的使用,包括解决驱动程序安装和版本匹配问题,以及如何配置谷歌和火狐浏览器进行后台静默启动。同时,提供了获取网页元素HTML的方法。

特殊说明:自动化测试工具必须安装驱动程序,否则会报错:

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

谷歌驱动下载:https://sites.google.com/a/chromium.org/chromedriver/home

驱动版本不匹配错误【最新版的驱动必须匹配最新版的谷歌浏览器】:

This version of ChromeDriver only supports Chrome version 81参考:http://www.codingwhy.com/view/5188.html

 

谷歌浏览器参考别人写的:https://www.cnblogs.com/yoyoketang/p/8078873.html

 

 

selenium 获取某个元素的html【如何支持获取伪元素:: before中的content内容】https://blog.youkuaiyun.com/weixin_43343144/article/details/106469298

 

谷歌浏览器后台启动配置方法

# 配置谷歌浏览器静默启动【后台启动】
chrome_options = webdriver.ChromeOptions()
# 谷歌浏览器headless前面的--可以不加【建议加上,代表命令行参数】
chrome_options.add_argument('--headless')
# 一定要把chrome_options配置来构造谷歌浏览器对象【必须匹配对应驱动的路径】
web_chrome = webdriver.Chrome(executable_path=r"D:\www\KirinProject\path\chromedriver.exe",options=chrome_options)
web_chrome.get("https://www.baidu.com")
print(web_chrome.page_source)
火狐浏览器后台启动配置方法:

from selenium import webdriver

firefox_options = webdriver.FirefoxOptions()
# 注:火狐浏览器headless前面一定要加--,代表命令行参数
firefox_options.add_argument('--headless')
webdriver.FirefoxOptions()
web_firefox = webdriver.Firefox(options=firefox_options)
web_firefox.get("http://www.baidu.com")
print(web_firefox.page_source)

其他浏览器原理差不多! 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值