zoukankan      html  css  js  c++  java
  • Selenium使用ChromeOptions启动参数

    Selenium是一个支持多平台+多浏览器+多版本的浏览器自动化测试框。
    Selenium在启动浏览器时可以通过desired_capbilities参数来指定需要启动的平台、浏览器、版本及浏览器启动参数。
    ChromeOptions是chromedriver支持的浏览器启动选项。

    from selenium import webdriver
    options = webdriver.ChromeOptions()
    

    options常用属性及方法为:

    • binary_location='':指定Chrome浏览器路径
    • debuger_address=':指定调试路径
    • headless: 无界面模式
    • add_argument():添加启动参数
    • add_extension:添加本地插件
    • add_experimental_option:添加实验选项
    • to_capablilities:将options转为标准的capablitiies格式

    通过add_argument()可以添加启动参数,如初始化窗口尺寸,隐身模式等。

    options.add_argument('headless')  # 无界面启动,也可以直接设置options.headless=True
    driver = webdriver.Chrome(options=options)    # 相当于 driver = webdriver.Chrome(desired_capabilities=options.to_capablities())
    

    然而由于Chrome浏览器及chromedriver版本不断更新,笔者发现有些参数在Mac 80版Chrome + 80版chromedriver中无法生效

    ChromeOptions参数参考

    生效的ChromeOptions参数

    # 无界面模式
    options.add_argument('headless')
    
    # 指定用户客户端-模拟手机浏览
    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"')
    
    # 禁用图片加载
    options.add_argument('blink-settings=imagesEnabled=false')
    
    # 隐身模式
    options.add_argument('incognito')
    
    # 自动打开开发者工具
    options.add_argument("auto-open-devtools-for-tabs")
    
    # 设置窗口尺寸,注意宽高之间使用逗号而不是x
    options.add_argument('window-size=300,600')
    
    # 设置窗口启动位置(左上角坐标)
    options.add_argument('window-position=120,0')
    
    # 禁用gpu渲染
    options.add_argument('disable-gpu')
    
    # 全屏启动
    options.add_argument('start-fullscreen')
    
    # 全屏启动,无地址栏
    options.add_argument('kiosk') 
    
     # 启动时,不激活(前置)窗口
    options.add_argument('no-startup-window') 
    ...
    

    无效的参数

    options.add_argument('--start-maximized')  # 最大化启动,无效
    options.add_argument('url=https://www.baidu.com/')  # 设置启动的url,无效
    options.add_argument('disable-infobars')  # 禁用inforbar,无效
    options.add_argument('hide-scrollbars')  # 隐藏滚动条,无效
    

    原因尚不清楚,有知道或解决方法的欢迎留言。

  • 相关阅读:
    自编游戏
    宣言
    Leetcode: 12. Integer to Roman
    Leetcode: 11. Container With Most Water
    Leetcode: 10. Regular Expression Matching
    网络编程:listen函数
    网络编程:connect函数
    Leetcode: 9. Palindrome Number
    Leetcode: 8. String to Integer (atoi)
    Leetcode: 7. Reverse Integer
  • 原文地址:https://www.cnblogs.com/superhin/p/12607074.html
Copyright © 2011-2022 走看看