Selenium+Headless Firefox

背景

今天本地调试基于Selenium+PhantomJS的动态爬虫程序顺利结束后,着手部署到服务器上,刚买的热乎的京东云,噼里啪啦一顿安装环境,最后跑的时候报了这么个错误:

UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead

运用我考了五遍才飘过的六级英语定睛一看,这个意思是说,新版本的Selenium不再支持PhantomJS了,请使用Chrome或Firefox的无头版本来替代。脑瓜里瞬间响起了这首歌的旋律,简直不能接受,凭什么就把我们PhantomJS抛弃了(╯‵□′)╯︵┻━┻。

因为这半年都没有写爬虫的需求,并且最近一直在本地用的老版本Selenium开发,所以一直还PhantomJS的很嗨,殊不知脚步已经落后了。查了一下,大概是去年七八月份Chrome和Firefox相继推出了无头浏览器模式。可能就因为这样,PhantomJS独领风骚的局面瞬间丧失,然后逐渐消失在历史的尘埃中吧……小厂出的创新产品,大厂做出类似产品之后,小厂GG,大概也是这么一回事吧……

Selenium+Headless Firefox

虽然很不情愿,但是人不能总是追忆过去的美好,该往前走的时候就要往前走对吧~

其实Selenium+Headless Firefox没什么好说的,跟Selenium+Friefox的区别就是实例化的时候传个参数而已。

需要注意的点就是:

本地要有Firefox,不然报找不到载体 本地要有geckodriver,最好再配置一下环境变量 没了

各个语言的示例代码都可以在这个链接里找到,这里就搬运一下python的示例代码吧:

from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait

if __name__ == "__main__":
    options = Options()
    options.add_argument(‘-headless‘)  # 无头参数
    driver = Firefox(executable_path=‘geckodriver‘, firefox_options=options)  # 配了环境变量第一个参数就可以省了,不然传绝对路径
    wait = WebDriverWait(driver, timeout=10)
    driver.get(‘https://www.google.com‘)
    wait.until(expected.visibility_of_element_located((By.NAME, ‘q‘))).send_keys(‘headless firefox‘ + Keys.ENTER)
    wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, ‘#ires a‘))).click()
    print(driver.page_source)
    driver.quit()

win10和ubuntu下我都测试了没问题,从前实例化一个PhantomJS大约3秒,Headless Firefox的话,7秒左右……其实无伤大雅。

这里友情提示一下新手小伙伴,别每下载一个网页实例化一个webdriver(Firefox or Chrome)然后就close()掉,实例化webdriver的时间也是时间~推荐将下载器做成单例类或将webdirver做类变量。

Selenium+Headless Chrome

这个跟上边大同小异,我没试,传送门:

PhantomJS在Selenium中被标记为过时的应对措施 Getting Started with Headless Chrome

原文地址:https://www.cnblogs.com/pythonClub/p/9736361.html

时间: 2024-08-03 08:15:35

Selenium+Headless Firefox的相关文章

python中用selenium调Firefox报错问题

python在用selenium调Firefox时报错: Traceback (most recent call last):  File "G:\python_work\chapter11\test_selenium_firefox.py", line 10, in <module>    driver = webdriver.Firefox()  File "C:\Python34\lib\site-packages\selenium\webdriver\fi

Selenium和firefox兼容性问题

Selenium和firefox兼容性问题 2016-07-10 若出现兼容性问题,会报如下错误: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms 解决方案见文章:http://www.webdriver.org/article-22-1.html 在selenium下载页面 http://www.seleniumh

phantomjs + selenium headless test

1. 安装selenium pip install selenium 2. 安装phantomjs 如果你是Ubuntu12.04,默认安装的版本是1.4.这个会出错. 需要安装1.9.7 cd /usr/local/share sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2 sudo tar xjf phantomjs-1.9.7-linux-x86_6

Selenium + Python + Firefox

按网上教程搭建好环境后,执行下面的代码出现了错误: 测试代码如下: ? 1 2 3 from selenium import webdriver driver=webdriver.Firefox() driver.get("http://www.baidu.com") 错误信息如下: Traceback (most recent call last): File "D:\pcode\24.py", line 2, in <module> driver=w

selenium启动Firefox失败

今天搭建java+selenium环境,搭建几次都失败,总结一下原因 1. selenium启动Firefox,不需要额外的driver 2. Friefox如果没有安装到默认路径C盘,代码中需要修改为: //如果火狐浏览器没有默认安装在C盘,需要制定其路径 System.setProperty("webdriver.firefox.bin","D:/Program Files (x86)/Mozilla Firefox/firefox.exe"); WebDriv

selenium启动firefox打开导入向导问题解决

操作系统:win8-64位 火狐版本:40.0.2 问题描述:selenium启动firefox时,每次启动都提示我导入其他浏览器的页签,如下图所示 解决方法一: 到firefox的profiles.ini所在文件中修改IsRelative=1->IsRelative=0 C:\Users\{用户名}\AppData\Roaming\Mozilla\Firefox\profiles.ini 再次启动即可正常打开. 备注:这种解决方案,火狐浏览器一旦重新启动后,又会出现烦人的导入向导,并不能彻底解

运行selenium 出现 Firefox can&#39;t find the file at chrome://src/content/.....

今天在跑selenium 自动化测试,运行了后台之后,想在火狐打开目的页面,结果火狐可以运行可是无法打开页面,还显示了标题的字符串:Firefox can't find the file at chrome://src/content/..... 谷歌找了很久得到的原因如下:火狐版本太新....(因为selenium rc是被历史淘汰的东西,现在都是用selenium 2.0了,所以selenium 1.0所用到的selenium rc 新版本的火狐支持不太好,在40+上的版本运行都会比较不愉快

彻底解决使用Selenium启动Firefox每次都出现导入向导的问题

想必刚接触Selenium做WEB自动化测试的时候,常常会遇到这种问题.每次使用Selenium启动firefox的时候都会出现"导入向导",只能通过手动点击取消掉用例才能继续跑下去. 这个问题困扰着每个自动化测试的初学者.江边望海在初期的时候也遇到了.下面是解决的思路. 很多初学者在遇到问题的时候往往希望直接百度,没有自己的思考.因此,造成了很多网上的解决办法直接治标不治本,误导了很多人.(后面会说到) 先说说我的解决思路吧. 1.使用Google而不是使用百度搜解决方法,用英文而不

selenium启动firefox时加载扩展

有些时候,我们测试需要用到插件或者已经导入的证书(比如金融和安全加密行业),而selenium启动firefox时会打开一个新的,不含有任何插件和个人证书的firefox(等同于全新安装后第一次打开的那个firefox)这种情况下,我们就要用firefoxprofile了. 我们需要先新建一个profile或者直接使用默认,最快捷的方法就是把默认的profile拷贝一份出来. 关于firefox的profile,官网有介绍,点击这里查看. 使用特定Profile启动,使用FirefoxDrive