如何在Windows应用自动化中集成undetected-chromedriver?
解决方案:集成undetected-chromedriver与解决登录弹窗空白问题
一、集成undetected-chromedriver实现微软账号登录
1. 安装依赖
通过pip安装适配本地Chrome版本的undetected-chromedriver包:
pip install undetected-chromedriver
2. 核心实现步骤
核心逻辑是先启动绕过反检测的Chrome实例,再用Appium触发应用内的登录按钮,最后切换到undetected Chrome完成登录流程,避免被微软风控拦截。
示例代码:
import undetected_chromedriver as uc from appium import webdriver import time # 1. 初始化undetected Chrome实例,配置必要参数 chrome_options = uc.ChromeOptions() # 允许弹窗、启用JS、禁用弹窗拦截 chrome_options.add_argument("--disable-popup-blocking") chrome_options.add_argument("--enable-javascript") # 可选:指定用户数据目录,保留登录状态(需提前手动登录一次) chrome_options.add_argument(r"--user-data-dir=C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data") # 启动绕过反检测的Chrome uc_driver = uc.Chrome(options=chrome_options) # 2. 连接WinAppDriver操作目标Windows应用 desired_caps = { "app": "C:\\Path\\To\\Your\\TargetApp.exe", # 或应用包名 "platformName": "Windows", "deviceName": "WindowsPC" } appium_driver = webdriver.Remote("http://127.0.0.1:4723", desired_caps) # 3. 触发应用内的微软登录按钮 appium_driver.find_element("xpath", "//*[@text='使用Microsoft账号登录']").click() time.sleep(2) # 等待Chrome窗口唤起 # 4. 切换到微软登录页面所在的Chrome窗口 for handle in uc_driver.window_handles: uc_driver.switch_to.window(handle) if "login.microsoftonline.com" in uc_driver.current_url: break # 5. 执行登录操作(元素定位需根据实际页面调整) uc_driver.find_element("id", "i0116").send_keys("your-microsoft-account@example.com") uc_driver.find_element("id", "idSIButton9").click() time.sleep(1) uc_driver.find_element("id", "i0118").send_keys("your-password") uc_driver.find_element("id", "idSIButton9").click() # 6. 登录完成后切回应用窗口,继续后续自动化 appium_driver.switch_to.window(appium_driver.window_handles[0])
注意事项
- 提前关闭所有系统Chrome进程,确保登录流程在undetected实例中进行
- 若版本不兼容,可指定Chrome主版本启动:
uc.Chrome(version_main=118)(替换为你的Chrome版本号) - 避免频繁更换IP或账号,降低被风控拦截的概率
二、解决登录弹窗空白问题
按以下顺序排查并修复:
- 网络验证:确认本地网络可正常访问
login.microsoftonline.com,关闭代理、防火墙或切换稳定网络测试 - Chrome权限调整:在
chrome_options中添加--disable-web-security(仅测试环境用)、--allow-running-insecure-content,确保页面资源加载不受限 - 窗口焦点处理:点击登录按钮后,通过
uc_driver.maximize_window()将Chrome窗口前置,避免因未激活导致加载异常 - 缓存清理:使用全新临时用户目录:
chrome_options.add_argument("--user-data-dir=C:\\Temp\\ChromeTempData"),排除缓存导致的加载失败 - 版本适配:卸载Chrome后安装稳定版,或让
undetected-chromedriver自动下载对应版本的驱动
内容的提问来源于stack exchange,提问作者sowmiya abi
相关产品推荐
相关产品推荐

