Python中Selenium实现Twitch授权遇浏览器不支持错误的解决求助
问题:使用Selenium实现Twitch授权时提示“浏览器不被支持”
在使用Python的Selenium实现Twitch授权时遇到阻碍,始终无法解决,错误提示为:
Your browser is not yet supported. Use the recommended browser or learn more
已尝试以下手段但无效:
- 使用多种不同浏览器
- 更换不同Twitch账号
- 使用undetected Selenium
附上当前代码:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.chrome.options import Options import time import json import re import os PATH = os.path.join(os.path.join(os.path.join(os.getcwd()))).replace('/','//') options = Options() options.headless = False options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36") options.add_argument('--ignore-ssl-errors') options.add_argument('--no-sandbox') options.add_argument("--disable-blink-features") options.add_argument("--disable-blink-features=AutomationControlled") driver = webdriver.Chrome(executable_path=r'{0}\\chromedriver.exe'.format(PATH),options=options) driver.get("https://www.twitch.tv/") driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/nav/div/div[3]/div[3]/div/div[1]/div[1]/button/div/div').click() self.driver.find_element_by_xpath('//*[@id="login-username"]').send_keys(login) self.driver.find_element_by_xpath('//*[@id="password-input"]').send_keys(password) self.driver.find_element_by_css_selector('button[data-a-target="passport-login-button"]').click()
修复方案
1. 彻底隐藏Selenium自动化特征
Twitch的反爬机制会检测多个自动化标识,仅禁用AutomationControlled不足以规避,需补充以下配置:
- 添加
--disable-dev-shm-usage避免共享内存异常 - 添加
--disable-extensions禁用浏览器扩展 - 通过CDP命令注入脚本,修改
navigator.webdriver属性为undefined
2. 正确使用undetected-chromedriver
确保安装最新版工具,它会自动处理大部分反检测逻辑:
pip install undetected-chromedriver
3. 匹配真实用户代理
用户代理需与当前使用的Chrome版本完全一致,可通过chrome://version/查看真实的用户代理字符串,避免版本不匹配被识别。
调整后的完整代码示例
import undetected_chromedriver as uc from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 配置浏览器选项 options = uc.ChromeOptions() options.headless = False options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-extensions") # 替换为你Chrome浏览器的真实用户代理 options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36") # 初始化undetected-chromedriver driver = uc.Chrome(options=options) driver.get("https://www.twitch.tv/") # 等待登录按钮加载并点击(用CSS选择器更稳定) WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-a-target="login-button"]')) ).click() # 等待登录表单加载 WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "login-username")) ) # 输入账号密码 username_input = driver.find_element(By.ID, "login-username") password_input = driver.find_element(By.ID, "password-input") username_input.send_keys("你的Twitch账号") password_input.send_keys("你的Twitch密码") # 提交登录 submit_btn = driver.find_element(By.CSS_SELECTOR, 'button[data-a-target="passport-login-button"]') submit_btn.click() # 若出现验证码,需手动处理或接入合规打码服务
额外注意事项
- 避免高频请求,使用
WebDriverWait替代固定time.sleep(),模拟真实操作节奏 - 尽量不要使用无头模式,Twitch对无头浏览器的检测强度更高
- 若账号存在异常风险,建议先通过真实浏览器登录验证账号状态
内容的提问来源于stack exchange,提问作者iPython
相关产品推荐
相关产品推荐

