from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
构建带有参数的谷歌浏览器
opt = Options()
opt.add_argument('--no-sandbox') #解决DevToolsActivePort文件不存在的报错
opt._arguments = ['disable-infobars'] #去掉谷歌浏览器正在被自动测试控制字样
opt.add_argument('window-size=1920x3000') #指定浏览器分辨率
opt.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
opt.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
# 不加载图片, 提升速度
opt.add_argument('blink-settings=imagesEnabled=false')
# 加载用户信息的谷歌浏览器
opt.add_argument("--user-data-dir="+r"C:\Users\Administrator\AppData\Local\Google\Chrome\User Data")
opt.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
# 添加UA
opt.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"')
opt.add_argument('--no-sandbox') #以最高权限运行
opt.add_argument("--disable-javascript") #禁用JavaScript
# 设置开发者模式启动,该模式下webdriver属性为正常值
opt.add_experimental_option('excludeSwitches', ['enable-automation'])
# 禁用浏览器弹窗
prefs = {
'profile.default_content_setting_values' : {
'notifications' : 2 }}
opt.add_experimental_option('prefs',prefs)
opt.add_argument('--disable-plugins') #禁止插件
# 禁用弹出拦截
opt.add_argument('--disable-pop

本文总结了使用Python+Selenium进行自动化测试时的一些核心命令,包括启动配置浏览器、设置窗口大小、等待时间、导航、元素定位与交互、iframe切换、滚动条控制、浏览器操作、页面信息获取、截图、表单提交、拖放操作、取消选择、对话框处理、层级定位、可见性检查、模拟鼠标和键盘动作,以及如何关闭浏览器。详细内容可查阅Selenium中文官方文档。
最低0.47元/天 解锁文章
592

被折叠的 条评论
为什么被折叠?



