You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium自动化Twitter(X)登录触发反机器人检测求助

解决Twitter登录自动化被反爬拦截的问题

核心原因

Twitter的反机器人系统会检测Selenium的特征(比如navigator.webdriver标识、机械的操作节奏、异常浏览器指纹),你的操作被判定为自动化行为,所以触发重定向。

具体解决方法

  • 隐藏Selenium自动化标识
    启动Chrome时添加专属参数,同时修改浏览器的navigator.webdriver属性,让系统无法识别这是自动化浏览器:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    # 禁用自动化控制标识
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    
    driver = webdriver.Chrome(options=options)
    # 修改navigator.webdriver值
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    
  • 优化操作节奏,模拟真人行为
    不要一次性完成输入和点击,加入随机延迟,拆分输入动作:

    import time
    import random
    from selenium.webdriver.common.action_chains import ActionChains
    
    driver.get(URLS.login)
    # 页面加载后先随机延迟
    time.sleep(random.uniform(2, 4))
    
    username_input = driver.find_element(By.NAME, 'text')
    # 逐个输入字符,每个字符间隔随机小延迟
    for char in username:
        username_input.send_keys(char)
        time.sleep(random.uniform(0.1, 0.3))
    
    # 定位更精准的Next按钮,避免误点其他元素
    next_button = driver.find_element(By.XPATH, '//div[@role="button" and contains(text(), "Next")]')
    # 先移动到按钮,延迟后再点击
    ActionChains(driver).move_to_element(next_button).perform()
    time.sleep(random.uniform(0.5, 1.2))
    next_button.click()
    
  • 优化浏览器指纹
    添加自定义User-Agent,模拟真实浏览器:

    options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
    

    还可以添加时区、语言参数:

    options.add_argument("--lang=en-US")
    options.add_argument("--timezone=America/New_York")
    
  • 改用JavaScript点击(备选方案)
    如果上述方法仍无效,尝试用JS执行点击(你提到控制台点击有效,结合隐藏标识后应该可行):

    driver.execute_script("arguments[0].click();", next_button)
    

额外注意事项

  • 不要频繁运行脚本,避免短时间内多次请求触发账号限制;
  • 确保使用的ChromeDriver版本和本地Chrome版本完全匹配;
  • 可以尝试使用undetected-chromedriver库(它会自动处理大部分Selenium特征隐藏),但注意不要过度依赖。

内容的提问来源于stack exchange,提问作者KumaTea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 06:15:04