Selenium automated testing, using ChromeOptions startup parameters

Selenium is a browser automation test box that supports multiple platforms + multiple browsers + multiple versions.
Selenium can specify the platform, browser, version and browser startup parameters that need to be started through the desired_capbilities parameter when starting the browser.
ChromeOptions is a browser startup option supported by chromedriver.

from selenium import webdriver
options = webdriver.ChromeOptions()

The commonly used attributes and methods of options are:

  • binary_location='': Specify the Chrome browser path
  • debuger_address=': specify the debug path
  • headless: headless mode
  • add_argument(): Add startup parameters
  • add_extension: add local plugin
  • add_experimental_option: add experimental option
  • to_capablities: Convert options to standard capablities format

You can add startup parameters through add_argument(), such as initializing window size, incognito mode, etc.

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

However, due to the continuous update of Chrome browser and chromedriver version, I found that some parameters cannot take effect in Chrome version 80 for Mac + chromedriver version 80

ChromeOptions parameter reference

Valid ChromeOptions parameters

# 无界面模式
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') 
...

invalid parameter

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')  # 隐藏滚动条,无效

The reason is not clear, welcome to leave a message if you know or solve it.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/m0_59868866/article/details/130466223