Study Address: http://blog.csdn.net/vinson0526/article/details/51850929
selenium
when using, we may need to chrome
do some special settings to complete the browser behavior we expect, such as 阻止图片加载
, and 阻止JavaScript执行
other actions. These need selenium
ChromeOptions
to help us complete
What is Chromeoptions
chromeoptions
is a convenient chrome
class for controlling the properties at startup. Through selenium
the source code, you can see, chromeoptions
mainly provides the following features:
- Set the chrome binary file location (binary_location)
- Add startup parameters (Add_argument)
- Add extension App (Add_extension, Add_encoded_extension)
- Add an experimental setting parameter (add_experimental_option)
- Set Debugger address (debugger_address)
customizing startup options
What we use most is three features.
- Add Chrome boot Parameters
- Modify Chrome Settings
- Add an extension app
The following python
example one by one shows that other languages can refer to the selenium source code
Add Chrome boot Parameters
# 启动时设置默认语言为中文 UTF-8from selenium import webdriveroptions = webdriver.ChromeOptions()options.add_argument(‘lang=zh_CN.UTF-8‘)driver = webdriver.Chrome(chrome_options = options)
The most common scenario is set up user-agent
to simulate mobile devices, such as analogiphone6
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"‘)
Modify Chrome Settings
# 禁止图片加载from selenium import webdriveroptions = webdriver.ChromeOptions()prefs = { ‘profile.default_content_setting_values‘ : { ‘images‘ : 2 }}options.add_experimental_option(‘prefs‘,prefs)driver = webdriver.Chrome(chrome_options = options)
For more experimental parameters, please refer to Chromedriver website.
Add extension
from selenium import webdriveroptions = webdriver.ChromeOptions()extension_path = ‘/extension/path‘options.add_extension(extension_path)driver = webdriver.Chrome(chrome_options = options)
Bonus Add Agent Method
from Selenium import webdriverproxy = "Proxy_host:proxy:port" Options = Webdriver. Chromeoptions () desired_capabilities = Options.to_capabilities () desired_capabilities[ Proxy '] = { "Httpproxy":P Roxy, "Ftpproxy":P Roxy, " Sslproxy ":P Roxy, " Noproxy ": none, " Proxytype ": " MANUAL ", " class ": "Org.openqa.selenium.Proxy", "AutoDetect": false}driver = Webdriver. Chrome (desired_capabilities = desired_capabilities)
Python Learning Notes Selenium customizing options for launching Chrome