zoukankan      html  css  js  c++  java
  • Python学习笔记之selenium 定制启动 chrome 的选项

    在自动化中,默认情况下我们打开的就是一个普通的纯净的chrome浏览器,而我们平时在使用浏览器时,经常就添加一些插件,扩展,代理之类的应用。所以使用 selenium 时,我们可能需要对 chrome 做一些特殊的设置,以完成我们期望的浏览器行为,比如阻止图片加载阻止JavaScript执行 等动作。这些需要 selenium的 ChromeOptions 来帮助我们完成。

    什么是 chromeoptions

    chromeoptions 是一个方便控制 chrome 启动时属性的类。chromeoptions 主要提供如下的功能:

    • 设置 chrome 二进制文件位置 (binary_location)
    • 添加启动参数 (add_argument)
    • 添加扩展应用 (add_extension, add_encoded_extension)
    • 添加实验性质的设置参数 (add_experimental_option)
    • 设置调试器地址 (debugger_address)

    定制启动选项

    以上的几个功能中我们最常用的是以下三个功能:

    • 添加chrome启动参数(add_argument)
    • 修改chrome设置(add_experimental_option)
    • 添加扩展应用(add_extension)

    下面以python为例一一说明.

    添加 chrome 启动参数

    # 启动时设置默认语言为中文 UTF-8
    from selenium import webdriver
    options = webdriver.ChromeOptions()
    options.add_argument('lang=zh_CN.UTF-8')
    # 启动浏览器最大化
    options.add_argument("--start-maximized")
    #最常用的应用场景是设置user-agent以用来模拟移动设备,比如模拟 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 Safari/601.1"')
    #模拟Android
    options.add_argument('user-agent="MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"') 
    
    driver = webdriver.Chrome(chrome_options = options)

    修改chrome设置

    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)

    添加扩展

    # 添加xpath helper应用
    from selenium import webdriver
    options = webdriver.ChromeOptions()
    
    # 设置好应用扩展
    extension_path = 'D:/extension/XPath-Helper_v2.0.2.crx'
    chrome_options.add_extension(extension_path)
    
    # 启动浏览器,并设置好wait
    browser = 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)
    下面来看个例子:
        chrome_driver = PATH_BROWSER_DRIVER_CHROME
        os.environ["webdriver.chrome.driver"] = chrome_driver
    #禁止弹出框
        prefs = {'profile.default_content_settings.popups': 0}
        options = webdriver.ChromeOptions()
        options.add_experimental_option('prefs', prefs)
        options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
        driver = webdriver.Chrome(chrome_driver, chrome_options=options)
    #等同于
        driver = webdriver.Chrome(executable_path=chrome_driver,chrome_options=options)
    参考地址:https://blog.csdn.net/zwq912318834/article/details/78933910

     

  • 相关阅读:
    用户界面线程(含有消息泵的线程)主线程与用户界面线程的通信
    poj 2151 Check the difficulty of problems 概率dp
    Codeforces 148D Bag of mice 概率dp
    [置顶] UVa在线比赛单题汇总DP专题
    UVa 10405 Longest Common Subsequence 最长公共子序列模板
    UVa 12018 Juice Extractor 切水果dp暂时存疑
    UVa 674 Coin Change 背包dp
    hdu 3853 LOOPS 圆环之理 概率dp
    UVa 111 History Grading 最长递增子序列 LIS
    UVa在线比赛单题汇总DP专题
  • 原文地址:https://www.cnblogs.com/fireporsche/p/7804423.html
Copyright © 2011-2022 走看看