微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐