1.参考

使用 headless chrome进行测试

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的更多相关文章

  1. selenium(六)Headless Chrome/Firefox--PhantomJS停止支持后,使用无界面模式。

    简介: 以前都用PhantomJS来进行无界面模式的自动化测试,或者爬取某些动态页面. 但是最近selenium更新以后,'Selenium support for PhantomJS has bee ...

  2. Python驱动Headless Chrome

    Headelss 比Headed的浏览器在内存消耗,运行时间,CPU占用都更具优势 from selenium import webdriverfrom selenium.webdriver.chro ...

  3. Selenium及Headless Chrome抓取动态HTML页面

    一般的的静态HTML页面可以使用requests等库直接抓取,但还有一部分比较复杂的动态页面,这些页面的DOM是动态生成的,有些还需要用户与其点击互动,这些页面只能使用真实的浏览器引擎动态解析,Sel ...

  4. 【python爬虫】利用selenium和Chrome浏览器进行自动化网页搜索与浏览

    功能简介:利用利用selenium和Chrome浏览器,让其自动打开百度页面,并设置为每页显示50条,接着在百度的搜索框中输入selenium,进行查询.然后再打开的页面中选中“Selenium - ...

  5. 爬虫(三)通过Selenium + Headless Chrome爬取动态网页

    一.Selenium Selenium是一个用于Web应用程序测试的工具,它可以在各种浏览器中运行,包括Chrome,Safari,Firefox 等主流界面式浏览器. 我们可以直接用pip inst ...

  6. selenium+headless chrome安装使用

    pip install selenium 因为phantomJS将停止维护,所以建议使用headless chromeChromeDriver is a separate executable tha ...

  7. 爬虫(四)Selenium + Headless Chrome爬取Bing图片搜索结果

    Bing图片搜索结果是动态加载的,如果我们直接用requests去访问页面爬取数据,那我们只能拿到很少的图片.所以我们使用Selenium + Headless Chrome来爬取搜索结果.在开始前, ...

  8. Web自动化之Headless Chrome测试框架集成

    使用Selenium操作headless chrome 推荐 简介 WebDriver是一个W3C标准, 定义了一套检查和控制用户代理(比如浏览器)的远程控制接口,各大主流浏览器来实现这些接口以便调用 ...

  9. 利用Selenium自动化web测试

    简介: Selenium 是一个没有正式指导手册的开源项目,这让测试人员的问题调查很费时间.本文为基于 Selenium 1.0(发布于 2009 年 6 月)的测试期间的常见问题提供最佳实践. 简介 ...

随机推荐

  1. 使用javascript调用com组件

    <html> <head> <title> 调用com组件的方法示例 </title> <script language="javasc ...

  2. a.py

    #!/usr/bin/python # -*- coding: UTF-8 -*- import os import sys import re import shutil import glob d ...

  3. CF449C:Jzzhu and Apples

    题意简述 给出正整数n,你要把1-n之间的正整数分成尽可能多组,使得每一组两个数的最大公约数大于1;输出能分成最多组的个数,并按任意顺序输出每组的两个数. 很妙的一道题. 首先我们考虑去处理每个质数的 ...

  4. 竞赛常用STL备忘录

    __builtin: __builtin_popcount:二进制中 1 的个数__builtin_ctz:末尾的 0,即对 lowbit 取log__builtin_clz:开头的 0,用 31 减 ...

  5. iperf3 不支持双工模式

    iperf 2.05的时候,客户端可以使用参数"-d"来进行双工测试,先测试发送,client向server发送数据,等到测试时间结束后(默认为10s,可以通过-t选项来更改),然 ...

  6. 阿里云主机Nginx下配置NodeJS、Express和Forever

    https://cnodejs.org/topic/5059ce39fd37ea6b2f07e1a3 AngularJS中文社区即运行在阿里云主机上,本站使用Nginx引擎,为了AngularJS,我 ...

  7. Light OJ 1095

    题意: 给你 N 个数, 总共有 N! 种排列, 现在 要你统计前 M 个数 刚好 有K 个数 在原来的位置上 的排列个数 思路: 首先 M 中选 K C(m,k): 则 共 剩下 n - k 个数, ...

  8. [PHP]一些坑

    一.如果有一个字符串变量等于'error',它跟0 == 运算时,会返回true,但是它并不是一个预置常量 $test = 'error'; var_dump($test == 0);//true 二 ...

  9. 实体类和json互相转换

    /// <summary> /// 将实体类转换为json数据 /// </summary> /// <returns></returns> publi ...

  10. Fiddler 抓包工具

    序章 Fiddler是一个蛮好用的抓包工具,可以将网络传输发送与接受的数据包进行截获.重发.编辑.转存等操作.也可以用来检测网络安全.反正好处多多,举之不尽呀!当年学习的时候也蛮费劲,一些蛮实用隐藏的 ...