python selenium浏览器自动化¶
0 背景¶

Python的Selenium是一个强大的工具,用于自动化Web浏览器操作。它广泛应用于网页测试、数据抓取和自动化任务。
本文主要是在一些基本的selenium操作之上,记录在部分场景下的其他操作。
1 使用¶
前置条件¶
驱动下载¶
chrome:
https://registry.npmmirror.com/binary.html?path=chromedriver/
firefox:
https://github.com/mozilla/geckodriver/releases
edge:
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
ie:
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
加载插件¶
设置代理¶
有时用于mitmproxy时用, windows在此之前导入证书 mitmproxy-ca-cert.p12
options = webdriver.ChromeOptions()
options.add_argument('--proxy-server=https://127.0.0.1:8000')
dr = webdriver.Chrome(options=options)
from selenium import webdriver
# 静态IP:102.23.1.105:2005
# 阿布云动态IP:http://D37EUYTR6VT4W2:[email protected]:9020
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:
options = webdriver.ChromeOptions()
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': r'E:\temp','download.prompt_for_download':False}
options.add_experimental_option('prefs', prefs)
dr = webdriver.Chrome(options=options)
截图并保存¶
设置无图模式¶
不显示图片
options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
prefs写法:
2表示不显示图片, 1表示显示图片.
或者
设置无头模式¶
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
dr = webdriver.Chrome(options=options)
窗口分辨率¶
最大化窗口
自定义窗口分辨率
隐藏滚动条¶
远程DEBUG¶
隐藏"正受到自动测试软件的控制"提示¶
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
或者
指定chromedriver路径¶
指定chrome程序路径¶
有时因为使用绿色版的Chrome程序, 但又不在环境变量里面, 因此可以指定chrome的路径
使用自定义userdata目录¶
可以是全新的文件夹, 也可以是chrome自己的
模拟移动设备¶
设置自定义User-Agent
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('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"')
避免密码提示框的弹出¶
options = webdriver.ChromeOptions()
prefs = {
'credentials_enable_service':False,
'profile.password_manager_enabled':False
}
options.add_experimental_option("prefs", prefs)
加载插件¶
options = webdriver.ChromeOptions()
extensionPath = 'D:/extension/XPath-Helper_v2.0.2.crx'
options.add_extension(extensionPath)
设置编码格式¶
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')
dr = webdriver.Chrome(chrome_options = options)
忽略https错误¶
cookie¶
清除cookies
携带cookie
执行js¶
# 通过 js 新打开一个窗口
driver.execute_script('window.open("https://www.baidu.com");')
# 通过 js 移动到最下
time.sleep(3) # 等待 页面加载/Ajax数据返回
driver.execute_script( "var q=document.documentElement.scrollTop=10000" )
# 通过 js 返回所有html
driver.execute_script( "return document.documentElement.outerHTML" )
2 关于¶
欢迎关注我的微信公众号¶
