Python Learning notes Selenium customizing options for launching Chrome

Source: Internet
Author: User

Study Address: http://blog.csdn.net/vinson0526/article/details/51850929

seleniumwhen 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

chromeoptionsis 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

Related Article

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.