selenium 定制启动 chrome 的选项
-
序
使用
selenium时,我们可能需要对chrome做一些特殊的设置,以完成我们期望的浏览器行为,比如阻止图片加载,阻止JavaScript执行等动作。这些需要selenium的ChromeOptions来帮助我们完成什么是 chromeoptions
chromeoptions是一个方便控制chrome启动时属性的类。通过selenium的源码,可以看到,chromeoptions主要提供如下的功能:- 设置 chrome 二进制文件位置 (binary_location)
- 添加启动参数 (add_argument)
- 添加扩展应用 (add_extension, add_encoded_extension)
- 添加实验性质的设置参数 (add_experimental_option)
- 设置调试器地址 (debugger_address)
定制启动选项
我们最常用的是三个功能
- 添加chrome启动参数
- 修改chrome设置
- 添加扩展应用
下面以
python为例一一说明,其他语言可以参考 selenium 源码添加 chrome 启动参数
# 启动时设置默认语言为中文 UTF-8
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument(‘lang=zh_CN.UTF-8‘)
driver = webdriver.Chrome(chrome_options = options)最常用的应用场景是设置
user-agent以用来模拟移动设备,比如模拟iphone6options.add_argument(‘user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"‘)修改chrome设置
# 禁止图片加载
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {
‘profile.default_content_setting_values‘ : {
‘images‘ : 2
}
}
options.add_experimental_option(‘prefs‘,prefs)
driver = webdriver.Chrome(chrome_options = options)更多实验参数请参考chromedriver 官网
添加扩展
from selenium import webdriver
options = webdriver.ChromeOptions()
extension_path = ‘/extension/path‘
options.add_extension(extension_path)
driver = webdriver.Chrome(chrome_options = options)附赠添加代理方法
from selenium import webdriver
PROXY = "proxy_host:proxy:port"
options = webdriver.ChromeOptions()
desired_capabilities = options.to_capabilities()
desired_capabilities[‘proxy‘] = {
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":None,
"proxyType":"MANUAL",
"class":"org.openqa.selenium.Proxy",
"autodetect":False
}
driver = webdriver.Chrome(desired_capabilities = desired_capabilities)
版权声明:本作品由掠雪墨影创作,采用知识共享署名 4.0 国际许可协议进行许可。转载请以链接形式标明本文地址。
转http://blog.csdn.net/vinson0526/article/details/51850929
selenium 定制启动 chrome 的选项的更多相关文章
- python selenium 定制启动Chrome的选项注意事项(十九)
自动化测试的时候为了避免每个case均需要登录的操作,所以把登录后的cookie信息保存下来,在有效期内使用cookie的方式实现登录操作,为了避免展现太多的登录操作,需要通过设置浏览器的option ...
- Python学习笔记之selenium 定制启动 chrome 的选项
在自动化中,默认情况下我们打开的就是一个普通的纯净的chrome浏览器,而我们平时在使用浏览器时,经常就添加一些插件,扩展,代理之类的应用.所以使用 selenium 时,我们可能需要对 chrome ...
- selenium 定制启动chrome的参数
selenium 定制启动chrome的参数 设置代理. 禁止图片加载 修改ua https://blog.csdn.net/vinson0526/article/details/51850929 1 ...
- selenium webdriver启动Chrome浏览器后无法输入网址的解决办法
通过selenium webdriver启动Chrome浏览器,脚本如下: from selenium import webdriver browser = webdriver.Chrome() br ...
- selenium python 启动Chrome
启动Chrom浏览器 下载chromedriver: http://chromedriver.storage.googleapis.com/index.html 当时找chromedriver与chr ...
- selenium+java启动chrome浏览器
- selenium启动Chrome时,加载用户配置文件
selenium启动Chrome时,加载用户配置文件 Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome ...
- selenium webdriver 启动三大浏览器Firefox,Chrome,IE
selenium webdriver 启动三大浏览器Firefox,Chrome,IE 1.安装selenium 在联网的情况下,在Windows命令行(cmd)输入pip install selen ...
- 【Selenium专题】WebDriver启动Chrome浏览器(二)
官方API Constructor Summary ChromeDriver() Creates a new ChromeDriver using the default server configu ...
随机推荐
- Effective Java - Item 1: Consider static factory methods instead of constructors
考虑使用静态工厂方法来替代构造方法, 这样的做的好处有四点. 1. 更好的表意 有的构造方法实际上有特殊的含义, 使用静态工厂方法能更好的表达出他的意思. 例如 BigInteger(int, int ...
- Spring Boot工程结构推荐程结构(最佳实践)
工程结构(最佳实践) Spring Boot框架本身并没有对工程结构有特别的要求,但是按照最佳实践的工程结构可以帮助我们减少可能会遇见的坑,尤其是Spring包扫描机制的存在,如果您使用最佳实践的工程 ...
- lync2013 错误: 已为不同的传输层安全性(TLS)目标找到类型为“McxInternal”且完全限定的域名(FQDN)为
最近 练习安装lync2013 在发布拓扑结构时遇到如下错误: lync 错误: 已为不同的传输层安全性(TLS)目标找到类型为“McxInternal”且完全限定的域名(FQDN)为“lync.co ...
- [leetcode]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...
- Hadoop家族学习路线图v
主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Ambari, Chukwa,新增加的项 ...
- jquery ajax 的 $.get()用法详解
js文件 $(document).ready(function(){ $("form").submit(function(event) {event.preventDefault( ...
- SQL Server 损坏修复 之一 常见错误解读
SQL Server 对数据库损坏的错误类型做了细化,在此对几个典型的错误作一下介绍. 错误信息是:“在文件 '%ls'中.偏移量为 %#016I64x 的位置执行 %S_MSG 期间,操作系统已经向 ...
- 介绍一些有趣的MySQL pager命令
一.分页结果集 在Linux系统中中,我们经常也会使用一些分页查看命令,例如less.more等.同样,MySQL客户端也提供了类似的命令,用来帮助我们对查询结果集进行分页.比如,SHOW ENGIN ...
- OpenNebula学习第二节OpenNebula Node Installation
一.准备工作 准备一台物理机或者虚拟机:Ubuntu16.0.4 已经更新了aliyun的source cpu支持虚拟化 关闭防火墙 二.安装步骤 2.1:下载opennebula repo源 wge ...
- Downloading jQuery 3.2.1
Downloading jQuery Compressed and uncompressed copies of jQuery files are available. The uncompresse ...