selenium设置火狐浏览器为headless(无头模式)

selenium已经停止了对PhantomJS的支持,只能调用Firefox或者Chrome浏览的无头模式(即没有浏览器界面)。
使用步骤:

安装Firefox浏览器
firefox历年版本安装包的官方镜像地址:
https://download-installer.cdn.mozilla.net/pub/firefox/releases/
安装geckodriver驱动
(1)下载geckodriver
火狐所有的geckodriver.exe驱动大全下载链接如下:https://github.com/mozilla/geckodriver/releases
(2)将geckodriver所在路径加入环境变量path当中
网上提到的有几种方法,一是将geckodriver.exe复制到Python的scripts目录,二是将geckodriver.exe复制到Firefox安装目录,然后再把Firefox安装目录加入到环境变量path中
编写代码

1
2
3
4
5
6
7
8
9
from selenium import webdriver
options = webdriver.FirefoxOptions()
#options.set_headless(True)
options.add_argument("--headless") #设置火狐为headless无界面模式
options.add_argument("--disable-gpu")
driver = webdriver.Firefox(options=options)
driver.get("https://www.qq.com")
driver.get_screenshot_as_file("test.png")
driver.quit()

  

posted @   CrossPython  阅读(739)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2018-10-20 VIM编码检查
2018-10-20 mysql 远程连接
2018-10-20 CentOS7下部署Django项目详细操作步骤
2018-10-20 Django之404、500、400错误处理
点击右上角即可分享
微信分享提示