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
以用来模拟移动设备,比如模拟iphone6
options.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 ...
随机推荐
- [转]小心PHP的类定义顺序与继承的问题
FROM : http://www.pakey.net/blog/php-class-shunxu.html 以下代码的运行环境均为PHP5.3.11先来看一段代码 <?php class A ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明 ...
- asp.net mvc Controller控制器返回类型
ASP.NET MVC包括了执行常见任务的ActionResult类型.这些类型罗列在表5-1中.每个类型都将在随后的小节中详细讨论. 表5-1 动作结果的类型及其说明 动作结果的类型 说 明 ...
- NOIP2016 酱油记
2016.11.17 考试前最后一个周四.然而我仍旧蒟蒻... 2016.11.18 周五,上午自家开车跑到晋城,中午12点到宾馆.下午4点去机房试机,先写了个线性筛,结果c++报错!?重开机,写个a ...
- Linux命令之du
Linux du命令用来查看文件或目录所占用磁盘空间的大小(Summarize disk usage of each FILE, recursively for directories.) 语法: d ...
- 混合开发 Hybird Ionic Angular Cordova web 跨平台 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- linux下性能分析命令[总结]
1.前言 在linux下开发程序,为了追求高性能,经常需要测试程序的性能,包括cpu.内存.io.网络等等使用情况.liunx下提供了众多命令方便查看各种资源的使用情况.经常用的有ps.top.fre ...
- c++ string wstring 字符串替换
int CStringTool::Replace(std::wstring& strContent, std::wstring& strReplace, std::wstring ...
- 什么是哈希码(HashCode)
什么是哈希码(HashCode) 在Java中,哈希码代表对象的特征. 例如对象 String str1 = “aa”, str1.hashCode= 3104 String str2 = “bb”, ...
- [Git] Move some commits to a separate branch that I have accidentally committed to master
Gosh no, I just added all of these commits to master. They were thought to be peer reviewed first in ...