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)
  • 1
  • 2
  • 3
  • 4
  • 5

最常用的应用场景是设置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"‘)
  • 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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

更多实验参数请参考chromedriver 官网

添加扩展

from selenium import webdriver
options = webdriver.ChromeOptions()
extension_path = ‘/extension/path‘
options.add_extension(extension_path)
driver = webdriver.Chrome(chrome_options = options)
  • 1
  • 2
  • 3
  • 4
  • 5

附赠添加代理方法

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

版权声明:本作品由掠雪墨影创作,采用知识共享署名 4.0 国际许可协议进行许可。转载请以链接形式标明本文地址。

转http://blog.csdn.net/vinson0526/article/details/51850929

时间: 2024-08-10 18:39:38

selenium 定制启动 chrome 的选项的相关文章

Python学习笔记之selenium 定制启动 chrome 的选项

学习地址:http://blog.csdn.net/vinson0526/article/details/51850929 使用 selenium 时,我们可能需要对 chrome 做一些特殊的设置,以完成我们期望的浏览器行为,比如阻止图片加载,阻止JavaScript执行 等动作.这些需要 selenium的 ChromeOptions 来帮助我们完成 什么是 chromeoptions chromeoptions 是一个方便控制 chrome 启动时属性的类.通过 selenium 的源码,

selenium启动Chrome时,加载用户配置文件

selenium启动Chrome时,加载用户配置文件 Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome配置的方法: 一.加载所有Chrome配置 用Chrome地址栏输入chrome://version/,查看自己的“个人资料路径”,然后在浏览器启动时,调用这个配置文件,代码如下: #coding=utf-8 from selenium import webdriver option = webdriv

selenium启动chrome出错处理:Message: 'chromedriver' executable needs to be in PATH

selenium启动chrome出错处理:Message: 'chromedriver' executable needs to be in PATH

早上在linux下用selenium启动Chrome时出现问题:

早上在linux下用selenium启动Chrome时出现问题:报错: Traceback (most recent call last): File "get2.py", line 62, in <module> browser = webdriver.Chrome() File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/chrome

设置Webdriver启动chrome为默认用户的配置信息

Webdriver 启动Chrome浏览器时,默认是打开一个新用户,而非默认用户,即新用户没有我们安装扩展程序.但在实际应用中,我们会需要 默认用户安装的一些扩展程序,比如对于某些js或者css样式,需要代理才能访问成功,使用默认用户就显得尤为重要(因为你不可能在新用户在安装扩展程序再继续测试). 如图: a)默认用户的扩展: 在锁定chrome的任务栏打开的状态: b) WebDriver打开的新用户的扩展: 在锁定chrome的任务栏打开的状态: ----------------------

selenium 无法启动IE浏览器的解决方法

需导入的支持类: 启动IE浏览器的代码: System.setProperty("webdriver.ie.driver", IEDriverServer.exe的存放路径); WebDriver driver = new InternetExplorerDriver(); driver.get("http://www.baidu.com"); driver.quit(); //关闭浏览器 如果以上代码运行时提示类似于下图的错误提示: 出现以上错误信息则表明是环境问

Java+selenium+Firefox/ IE/ Chrome主流浏览器自动化环境搭建

一.java+selenium+firefox 1.环境准备:JDK1.8 2.安装firefox浏览器v59 3.下载驱动:https://github.com/mozilla/geckodriver/releases 4.selenium依赖pom.xml导入:https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java selenium 历史版本镜像下载地址:https://npm.taobao.org/m

Selenium分离式启动Webdriver服务和浏览器

Selenium在启动浏览器时实际进行里两步操作, 通过子进程,使用chromedriver启动一套Webdriver服务 使用webdriver.Remote()方法连接该服务并发送指令启动浏览器. 在某些情况下如果,你需要分离式启动服务和连接服务的过程,可以使用以下步骤,参考代码如下: from selenium import webdriver from selenium.webdriver.chrome.service import Service # 导入chrome的服务启动方法 #

selenium无法调用chrome或者firefox的原因

现在我们用的开源工具一般为selenium,当然其中的好处是可以调用不同的浏览器.包含了ie\google\firefox等等,但调用的时候会出现无法调用其浏览器.原因很简单,selenium和其浏览器的版本不符合.一般情况下是selenium调用的各个DRIVER版本低于浏览器的版本引起. 具体情况: C:/Ruby200/lib/ruby/gems/2.0.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdr iver/remote/resp