PythonUI automated testing - browser startup parameter settings

The articles on the Internet are not friendly to beginners. They all give you a bunch of parameters without teaching you how to use them. They directly skip the most important part. I wrote this article in the hope that it will be a guide for others in the future.

Startup method when no parameters are set

import time
from selenium import webdriver

# 创建浏览器驱动参数对象
options = webdriver.ChromeOptions()

# 加载浏览器驱动
driver = webdriver.Chrome(options=options)

# 在浏览器中输入百度网址
driver.get('https://www.baidu.com/')
# 点击“百度一下”按钮
driver.find_element('xpath', '//*[@id="su"]').click()

Use incognito mode when launching your browser

import time
from selenium import webdriver

# 创建浏览器驱动参数对象
options = webdriver.ChromeOptions()

# 启动浏览器时,使用无痕模式
options.add_argument('--incognito')

# 加载浏览器驱动
driver = webdriver.Chrome(options=options)

Run with maximized window when starting browser

import time
from selenium import webdriver

# 创建浏览器驱动参数对象
options = webdriver.ChromeOptions()

# 启动浏览器时,使用无痕模式
options.add_argument('--incognito')
# 最大化窗口运行
options.add_argument('--start-maximized')

# 加载浏览器驱动
driver = webdriver.Chrome(options=options)

The remaining parameters are pretty much the same, you should all know how to use them (some may not be used)

.add_argument('--disable-infobars') 禁止策略化
.add_argument('--no-sandbox') 解决DevToolsActivePort文件不存在的报错
.add_argument('window-size=1920x3000') 指定浏览器分辨率
.add_argument('--disable-gpu') 谷歌禁用GPU加速
.add_argument('--disable-javascript') 禁用javascript
.add_argument('--incognito') 隐身模式(无痕模式)
.add_argument('--start-maximized') 最大化运行(全屏窗口),不设置,取元素会报错
.add_argument('--hide-scrollbars') 隐藏滚动条, 应对一些特殊页面
.add_argument('blink-settings=imagesEnabled=false') 不加载图片, 提升速度
.add_argument('--headless') 浏览器不提供可视化页面(无头模式). linux下如果系统不支持可视化不加这条会启动失败
.add_argument('lang=en_US') 设置语言
.add_argument('User-Agent=xxxxxx') 设置User-Agent属性
.add_argument('--kiosk-printing') 默认打印机进行打印
.binary_location = r"...\chrome.exe" 手动指定使用的浏览器位置
.add_experimental_option("debuggerAddress", "127.0.0.1:9222") 调用原来的浏览器,不用再次登录即可重启

prefs = {"":""}
prefs["credentials_enable_service"] = False
prefs["profile.password_manager_enabled"] = False
.add_experimental_option("prefs", prefs)
设置prefs属性,屏蔽'保存密码'提示框
.add_experimental_option('excludeSwitches', ['enable-automation']) 以开发者模式启动调试chrome,可以去掉提示受到自动软件控制
.add_experimental_option('useAutomationExtension', False) 去掉提示以开发者模式调用

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/myh919/article/details/132761527