Selenium Custom launch chrome option _selenium

Source: Internet
Author: User
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) 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.