【转】利用 selenium 的 webdrive 驱动 headless chrome
1.参考
2.概念
Headless模式解决了什么问题: 自动化工具例如 selenium 利用有头浏览器进行测试,面临效率和稳定性的影响,所以出现了 Headless Browser, 3年前,无头浏览器 PhantomJS 已经如火如荼出现了,紧跟着 NightmareJS 也成为一名巨星。无头浏览器带来巨大便利性:页面爬虫、自动化测试、WebAutomation... 用过PhantomJS的都知道,它的环境是运行在一个封闭的沙盒里面,在环境内外完全不可通信,包括API、变量、全局方法调用等。
So, Chrome59 推出了 headless mode,Chrome59版支持的特性,全部可以利用:
ES2017
ServiceWork(PWA测试随便耍)
无沙盒环境
无痛通讯&API调用
无与伦比的速度
https://developers.google.com/web/updates/2017/04/headless-chrome Getting Started with Headless Chrome
https://jiayi.space/post/zai-ubuntufu-wu-qi-shang-shi-yong-chrome-headless 在ubuntu服务器上使用Chrome Headless 845
UA有所差异:User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/63.0.3239.84 Safari/537.36
解决办法:chrome_options.add_argument("user-agent='Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'")
https://0x0d.im/archives/headless-browser-detection.html 无头浏览器异闻录
我们如何区分这些无头浏览器和正常的浏览器呢?从 Server Side 分析用户行为进行检测是一劳永逸的方法,但成本和难度都很大。 不过通过无头浏览器的一些特性,我们也可以从 Client Side 找出一些不同来。
3.代码
#coding:utf-8
from selenium import webdriver url = "http://demo.testfire.net"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path='/Users/xxxx/driver/chromedriver') driver.get('http://demo.testfire.net')
driver.find_element_by_xpath('//*[@id="_ctl0__ctl0_LoginLink"]').click()
driver.find_element_by_xpath('//*[@id="uid"]').clear()
driver.find_element_by_xpath('//*[@id="uid"]').send_keys('admin')
driver.find_element_by_xpath('//*[@id="passw"]').send_keys('admin')
driver.find_element_by_xpath('//*[@id="login"]/table/tbody/tr[3]/td[2]/input').click() print driver.current_url
最后 print 出登录成功的当前 url:http://demo.testfire.net/bank/main.aspx
20171227 更新:仅设置参数 '--headless' 会报错,而仅设置参数 '--disable-gpu' 则会自动补充 '--headless'
63.0.3239.84(正式版本) (64 位)
In [43]: from selenium import webdriver In [44]: chrome_options = webdriver.ChromeOptions() In [45]: chrome_options.add_argument('--headless') In [46]: driver = webdriver.Chrome(chrome_options=chrome_options)
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:63614/session {"capabilities": {"alwaysMatch": {"platform": "ANY", "browserName": "chrome", "version": "", "chromeOptions": {"ar
gs": ["--headless"], "extensions": []}}, "firstMatch": []}, "desiredCapabilities": {"platform": "ANY", "browserName": "chrome", "version": "", "chromeOptions": {"args": ["--headless"], "extensions": [
]}}} DevTools listening on ws://127.0.0.1:12481/devtools/browser/aa60bb34-e8a5-4740-909e-aa3e6f315376
[1227/173400.592:ERROR:gpu_main.cc(164)] Exiting GPU process due to errors during initialization
[1227/173400.606:ERROR:browser_gpu_channel_host_factory.cc(107)] Failed to launch GPU process.
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request In [47]: chrome_options.add_argument('--disable-gpu') In [48]: driver = webdriver.Chrome(chrome_options=chrome_options)
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:63672/session {"capabilities": {"alwaysMatch": {"platform": "ANY", "browserName": "chrome", "version": "", "chromeOptions": {"ar
gs": ["--headless", "--disable-gpu"], "extensions": []}}, "firstMatch": []}, "desiredCapabilities": {"platform": "ANY", "browserName": "chrome", "version": "", "chromeOptions": {"args": ["--headless",
"--disable-gpu"], "extensions": []}}} DevTools listening on ws://127.0.0.1:12452/devtools/browser/c175a776-71be-40c0-aa73-a64c253f1cb0
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request In [49]: driver.get('http://httpbin.org')
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:63672/session/026f8a02c2cd9bf7c7688c6b2934cd66/url {"url": "http://httpbin.org", "sessionId": "026f8a02c2cd9bf7c7688c6b2934cd66"
}
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
xx
In [56]: chrome_ops = webdriver.ChromeOptions() In [57]: chrome_ops.add_argument('--disable-gpu') In [58]: dri = webdriver.Chrome(chrome_options=chrome_options)
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:64092/session {"capabilities": {"alwaysMatch": {"platform": "ANY", "browserName": "chrome", "version": "", "chromeOptions": {"ar
gs": ["--headless", "--disable-gpu"], "extensions": []}}, "firstMatch": []}, "desiredCapabilities": {"platform": "ANY", "browserName": "chrome", "version": "", "chromeOptions": {"args": ["--headless",
"--disable-gpu"], "extensions": []}}} DevTools listening on ws://127.0.0.1:12713/devtools/browser/b873f662-1ca6-4cd0-a6cd-f9bd13d7e236
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
【转】利用 selenium 的 webdrive 驱动 headless chrome的更多相关文章
- selenium(六)Headless Chrome/Firefox--PhantomJS停止支持后,使用无界面模式。
简介: 以前都用PhantomJS来进行无界面模式的自动化测试,或者爬取某些动态页面. 但是最近selenium更新以后,'Selenium support for PhantomJS has bee ...
- Python驱动Headless Chrome
Headelss 比Headed的浏览器在内存消耗,运行时间,CPU占用都更具优势 from selenium import webdriverfrom selenium.webdriver.chro ...
- Selenium及Headless Chrome抓取动态HTML页面
一般的的静态HTML页面可以使用requests等库直接抓取,但还有一部分比较复杂的动态页面,这些页面的DOM是动态生成的,有些还需要用户与其点击互动,这些页面只能使用真实的浏览器引擎动态解析,Sel ...
- 【python爬虫】利用selenium和Chrome浏览器进行自动化网页搜索与浏览
功能简介:利用利用selenium和Chrome浏览器,让其自动打开百度页面,并设置为每页显示50条,接着在百度的搜索框中输入selenium,进行查询.然后再打开的页面中选中“Selenium - ...
- 爬虫(三)通过Selenium + Headless Chrome爬取动态网页
一.Selenium Selenium是一个用于Web应用程序测试的工具,它可以在各种浏览器中运行,包括Chrome,Safari,Firefox 等主流界面式浏览器. 我们可以直接用pip inst ...
- selenium+headless chrome安装使用
pip install selenium 因为phantomJS将停止维护,所以建议使用headless chromeChromeDriver is a separate executable tha ...
- 爬虫(四)Selenium + Headless Chrome爬取Bing图片搜索结果
Bing图片搜索结果是动态加载的,如果我们直接用requests去访问页面爬取数据,那我们只能拿到很少的图片.所以我们使用Selenium + Headless Chrome来爬取搜索结果.在开始前, ...
- Web自动化之Headless Chrome测试框架集成
使用Selenium操作headless chrome 推荐 简介 WebDriver是一个W3C标准, 定义了一套检查和控制用户代理(比如浏览器)的远程控制接口,各大主流浏览器来实现这些接口以便调用 ...
- 利用Selenium自动化web测试
简介: Selenium 是一个没有正式指导手册的开源项目,这让测试人员的问题调查很费时间.本文为基于 Selenium 1.0(发布于 2009 年 6 月)的测试期间的常见问题提供最佳实践. 简介 ...
随机推荐
- PCM EQ DRC 音频处理
PCM Pulse-code modulation的缩写,中文译名是脉冲编码调制.(I2S仅仅是PCM的一个分支,接口定义都是一样的, I2S的采样频率一般为44.1KHZ和48KHZ做,PCM采样频 ...
- 题解-ZeroJudge-c686 高斯符號
Problem ZeroJudge Solution 考慮到\(\lfloor \frac {km}n\rfloor\)等同於\(km\)整除\(n\),換種表示方法就是\(km\)減去\(km\)模 ...
- zabbix批量监控urls的状态码
.添加url监控脚本 [root@node1 usr]# vim /usr/local/zabbix_agents_3.2.0/scripts/web_site_code_status.sh #!/b ...
- windows连接服务端的域名正常,linux却不通,(针对于负载均衡后端节点设置)
1.初步判断不是网络上的,因为windows主机访问正常, 2.修改客户端linux主机 net.ipv4.tcp_tw_recycle=0,测试是否正常,(服务器当连接数达到一定量之后,会执行rec ...
- 手把手教你React Native 实战之开山篇《一》
先说一下我为什么学习RN 18年3月29号,随着自己内心的欲望和冲动,任务交接了一下,正式离开一家医疗公司.第二天就入职了这之前已经找好的公司,由于自己对代码浓厚的热情,自己终于也不再带团队.正好有充 ...
- IOS 常遇到的报错警告 以及 解决办法
1. This application is modifying the autolayout engine from a background thread, which can lead to ...
- Confluence 6 为发送邮件配置服务器
配置你的 Confluence 服务器发送电子邮件消息能够允许你的 Confluence 用户: 接受邮件通知和每天更新报表. 通过电子邮件发送一个页面. 你可以通过配置 'From' 字段中的内容来 ...
- day07 元组类型 字典类型 集合
元组:元组就是一个不可变的列表 1.用途:当我们需要记录多个同种属性的值,并且只有读的需求,没有改的需求,应该用元组. 2.定义方式:在()内用逗号分隔开多个任意类型的元素 t=(‘egon’)#注意 ...
- unity 3D 学习笔记
1.父对象的初始位置设,即刚开始的空对象的根节点位置应当设置成(0,0,0) 这样设置可以避免以后出现奇怪的坐标. GameObject实际上就是一些组件的容器. unity 使用公用变量原因是,在U ...
- django----利用Form 实现两次密码输入是否一样 ( 局部钩子和全局钩子 )
from django import forms # 导入表单模块 from django.core.exceptions import ValidationError class RegisterF ...