使用 selenium
时,咱们可能须要对 chrome
作一些特殊的设置,以完成咱们指望的浏览器行为,好比阻止图片加载
,阻止JavaScript执行
等动做。这些须要 selenium
的 ChromeOptions
来帮助咱们完成python
chromeoptions
是一个方便控制 chrome
启动时属性的类。经过 selenium
的源码,能够看到,chromeoptions
主要提供以下的功能:git
咱们最经常使用的是三个功能github
下面以python
为例一一说明,其余语言能够参考 selenium 源码web
# 启动时设置默认语言为中文 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
chrome
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"‘)
# 禁止图片加载 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 国际许可协议进行许可。转载请以连接形式标明本文地址。markdown
转http://blog.csdn.net/vinson0526/article/details/51850929iphone