Selenium自动化登录Google Cloud遇网络及认证错误求助
解决Selenium Wire + Chrome登录GCP的认证与代理连接错误
问题描述
使用Selenium及Selenium Wire编写Google Cloud门户登录流程时,输入账号点击继续后触发两类错误:
Request has invalid authentication credentials. Expected OAuth 2 access token, login cookie or other...net::ERR_PROXY_CONNECTION_FAILED
手动使用相同凭证操作无网络问题,Firefox驱动可正常运行,此前Chrome也曾正常工作,怀疑缓存问题导致登录时的网络及认证异常。
现有配置
Chrome驱动代码
from seleniumwire import webdriver from seleniumwire.webdriver import ChromeOptions def test_gcp_flow(): options = ChromeOptions() options.add_experimental_option("detach", True) options.add_argument('--no-sandbox') options.add_argument('--single-process') options.add_argument('--disable-dev-shm-usage') options.add_argument("--start-maximized") options.add_argument('--auto-open-devtools-for-tabs') options.add_argument('--log-level=2') options.add_argument('--disable-features=IsolateOrigins,site-per-process') options.add_argument("--ignore_ssl") options.add_argument('--ignore-ssl-errors') options.add_argument('--ignore-certificate-errors') options.add_argument("--disable-extensions") options.add_argument("--disable-setuid-sandbox") options.add_argument("--dns-prefetch-disable") options.add_argument('ignore-certificate-errors') options.add_argument('disable-web-security') options.add_argument('--allow-insecure-localhost') driver = webdriver.Chrome(options=options) driver.get('....any-hidden-url') # more flow actions - then it open gcp portal
openssl.cnf配置
openssl_conf = openssl_init [openssl_init] ssl_conf = ssl_sect [ssl_sect] system_default = system_default_sect [system_default_sect] Options = UnsafeLegacyRenegotiation
排查与修复方案
1. 清理Chrome缓存与用户数据
直接指定全新用户数据目录,彻底避免旧缓存、无效会话凭证干扰:
options.add_argument('--user-data-dir=/tmp/chrome_gcp_clean_profile') options.add_argument('--profile-directory=Default')
2. 解决Selenium Wire代理冲突
net::ERR_PROXY_CONNECTION_FAILED多由Selenium Wire自带代理拦截GCP认证请求导致,可绕过GCP相关域名的代理:
driver.seleniumwire_options['exclude_hosts'] = [ 'accounts.google.com', 'console.cloud.google.com', '*.googleapis.com' ]
3. 精简Chrome启动参数
过多参数(如disable-web-security)会破坏Chrome安全上下文,导致GCP OAuth认证逻辑失效,保留必要参数即可:
options = ChromeOptions() options.add_experimental_option("detach", True) options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument("--start-maximized") options.add_argument('--ignore-certificate-errors') options.add_argument("--disable-extensions") # 添加干净用户目录 options.add_argument('--user-data-dir=/tmp/chrome_gcp_clean_profile')
4. 验证SSL配置兼容性
UnsafeLegacyRenegotiation可能与Chrome SSL策略冲突,可临时恢复默认openssl配置,或在Selenium Wire中指定SSL证书:
driver.seleniumwire_options['ssl'] = { 'certfile': '/path/to/your/cert.pem', 'keyfile': '/path/to/your/key.pem', }
5. 检查Chrome版本匹配
确保Chrome浏览器与ChromeDriver版本完全一致,版本不匹配会引发各类网络、交互异常。
内容的提问来源于stack exchange,提问作者Manspof
相关产品推荐
相关产品推荐

