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
Selenium+Headless Firefox的更多相关文章
- 爬虫(三)通过Selenium + Headless Chrome爬取动态网页
一.Selenium Selenium是一个用于Web应用程序测试的工具,它可以在各种浏览器中运行,包括Chrome,Safari,Firefox 等主流界面式浏览器. 我们可以直接用pip inst ...
- selenium启动firefox时加载扩展
有些时候,我们测试需要用到插件或者已经导入的证书(比如金融和安全加密行业),而selenium启动firefox时会打开一个新的,不含有任何插件和个人证书的firefox(等同于全新安装后第一次打开的 ...
- selenium和Firefox版本不兼容
selenium8.py coding = utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.get(' ...
- selenium - Headless Browser and scraping - solutions - Stack Overflow
yum install pygtk2-devel selenium - Headless Browser and scraping - solutions - Stack Overflow Hea ...
- 解决selenium驱动Firefox跳转页慢慢慢的问题
首先我给自己定义为是一个更新偏执狂.不知道从哪个版本开始,使用selenium驱动打开Firefox浏览器时,会跳转到官网指定页,这个过程真是慢得要死. 为了解决这个问题,我是查了很多资料,解决方案是 ...
- python中用selenium调Firefox报错问题
python在用selenium调Firefox时报错: Traceback (most recent call last): File "G:\python_work\chapter11 ...
- selenium启动Firefox失败
今天搭建java+selenium环境,搭建几次都失败,总结一下原因 1. selenium启动Firefox,不需要额外的driver 2. Friefox如果没有安装到默认路径C盘,代码中需要修改 ...
- Selenium和firefox兼容性问题
Selenium和firefox兼容性问题 2016-07-10 若出现兼容性问题,会报如下错误: org.openqa.selenium.firefox.NotConnectedException: ...
- Selenium在Firefox中踩过的
本文转至 http://www.51testing.com/html/11/n-3711311.html,作者对webdriver在Firefox中设置profile配置项挺熟的,是用Python实现 ...
随机推荐
- haproxy json 自定义格式日志 并用rsyslog 收集
haproxy的配置 日志投送: global log 127.0.0.1 local1 info 默认端口是udp514 默认日志: defaults log global log-format \ ...
- Dockerfile详解(一)
Dockerfile 用于自动化构建一个docker镜像.Dockerfile里有 CMD 与 ENTRYPOINT 两个功能咋看起来很相似的指令,开始的时候觉得两个互用没什么所谓,但其实并非如此: ...
- ida脚本学习
#!/usr/bin/env python #coding:utf-8 from idc import * import idaapi import idautils import os os.sys ...
- cocos2dx JS 清除缓存重新编译打包安卓apk
复制他人工程时打包出错,无法进行.或者是资源缓存问题需要重新编译删除 proj.android 工程下的三个文件夹 frameworks -> runtime-src -> proj.an ...
- cocos2dx 3.x(for 循环让精灵从中间往上下两边排列)
最近很多游戏都喜欢房卡类的游戏,就是创建房间时(),选择玩法与规则,今天耗费2小时处理这个数学问题:例如选择规则两条,则背景框中间显示两条规则,若选择三条,则背景框中间显示三条规则与玩法,依次从中间往 ...
- ECshop后台新功能权限添加
ecshop后台新功能权限的添加 1.在后台“推荐管理”里添加“推荐人分成”.“会员分成”两个操作功能以及权限 index.php?act=menu incluedes/inc_priv.php:权限 ...
- IntelliJ IDEA 2017.3/2018.1 激活
传统的License Server方式已经无法注册IntelliJ IDEA2017.3的版本了. http://idea.lanyus.com,这个网站有破解补丁和注册码两种方式,另外http:// ...
- 导入转储文件的时候:Error Code: 1406. Data too long for column - MySQL
MySQL will truncate any insert value that exceeds the specified column width. to make this without e ...
- Comparator与Comparable用法与区别
一.概述. Comparator和Comparable两者都属于集合框架的一部分,都是用来在对象之间进行比较的,但两者又有些许的不同,我们先通过一个例子来看一下他们的区别,然后再分别学习下它们的源 ...
- ubuntu安装rvm
sudo apt-get install curl git-core bash -s stable < <(curl -s https://raw.github.com/wayneeseg ...