Order
When using selenium, we may need to make some special settings for Chrome to accomplish the browser behavior we expect, such as blocking pictures from loading, preventing JavaScript from executing, and so on. These need selenium chromeoptions to help us accomplish what is chromeoptions
Chromeoptions is a class that facilitates the control of chrome startup properties. Through the selenium source, you can see that chromeoptions mainly provides the following features: Set up the Chrome binary file location (binary_location) Add startup parameters (add_argument) Add extension application (Add_exten Sion, Add_encoded_extension) Add experimental setting parameters (Add_experimental_option) Set debugger address (debugger_address) customizing startup options
Our most common is three features add chrome start parameters modify Chrome settings Add extended Application
The following Python example one by one illustrates that other languages can refer to Selenium source code to add Chrome boot parameters
# Set the default language at startup for Chinese UTF-8 from
Selenium import webdriver
options = Webdriver. Chromeoptions ()
options.add_argument (' LANG=ZH_CN. UTF-8 ')
driver = Webdriver. Chrome (chrome_options = options)
The most commonly used scenario is to set up user-agent to simulate mobile devices, such as analog 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 "')
Modify Chrome Settings
# Prohibit picture loading 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)
More experimental parameters please refer to chromedriver website add extension
From selenium import webdriver
options = Webdriver. Chromeoptions ()
extension_path = '/extension/path '
options.add_extension (extension_path)
Driver = Webdriver. Chrome (chrome_options = options)
Bonus Add Agent Method
from selenium import webdriver PROXY = "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.seleniu M.proxy "," AutoDetect ": False} Driver = Webdriver. Chrome (desired_capabilities = desired_capabilities)