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

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

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.csdn.net/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)

其他浏览器原理差不多! 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值