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 ...
随机推荐
- [转]pear windows 安装
FROM : http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html 因为想使用phpdocument生成文档,不得不安装pear ...
- [转]Mysql中的SQL优化与执行计划
From : http://religiose.iteye.com/blog/1685537 一,如何判断SQL的执行效率? 通过explain 关键字分析效率低的SQL执行计划. 比如: expla ...
- 华硕 RT-AC54U路由器固件功能说明
引言 华硕 RT-AC54U这款路由器固件,界面做的非常不错(起码比OpenWrt要好看).功能也比較强悍,可是对于刚入手这个固件的用户可能会对此固件的一些非常好用的功能无从下手,所以这里我就写下了这 ...
- Guava之Iterables使用示例
这是一个常量工具类.Iterables类包含了一系列的静态方法,来操作或返回Iterable对象. public final class Iterables { private Iterables() ...
- 排序算法的实现(归并,快排,堆排,希尔排序 O(N*log(N)))
今天跟着左老师的视频,理解了四种复杂度为 O(N*log(N))的排序算法,以前也理解过过程,今天根据实际的代码,感觉基本的算法还是很简单的,只是自己写的时候可能一些边界条件,循环控制条件把握不好. ...
- SharePoint 2016 IT Preview的新feature列表
大致看了一下, 其中关于存储的部分貌似没有. 感觉Hybrid in SharePoint 2016未来可以做的事情比较多吧, 必须赶紧弄个cloud的东西开始玩玩了. Search的部署的更新, 以 ...
- 为 hexo NexT 添加 Gitment 评论插件
Gitment 是作者imsun实现的一款基于 GitHub Issues 的评论系统. 支持在前端直接引入, 不需要任何后端代码. 可以在页面进行登录, 查看, 评论, 点赞等操作. 同时有完整的 ...
- [Algorithm] Binary tree: Level Order Traversal
function Node(val) { return { val, left: null, right: null }; } function Tree() { return { root: nul ...
- maven 管理
http://www.cnblogs.com/qq78292959/p/3711501.html
- jQuery 操作 radio、select、checkbox
<script type="text/javascript"> $(function () { 一.radio 1.获取选中值,三种方法都可以: $('input:ra ...