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

使用Selenium+ChromeDriver登录TikTok遇“尝试次数过多”问题求助

解决ChromeDriver登录TikTok触发"尝试次数过多"的问题

TikTok的反爬机制会识别ChromeDriver的自动化特征,这就是手动浏览器能正常登录但自动化脚本不行的核心原因。以下是针对性的解决方案:

1. 隐藏WebDriver自动化核心特征

ChromeDriver默认会暴露navigator.webdriver属性及其他自动化标记,需通过配置和脚本彻底隐藏:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from fake_useragent import UserAgent

USER_AGENT = UserAgent()

def login(username, password):
    # Selenium 4+推荐使用Service替代executable_path
    service = Service('./tiktok/chromedriver')
    options = webdriver.ChromeOptions()
    
    # 关键:禁用自动化控制检测
    options.add_argument('--disable-blink-features=AutomationControlled')
    # 禁用自动化扩展及信息栏提示
    options.add_argument('--disable-extensions')
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    # 随机生成真实UserAgent
    options.add_argument(f'user-agent={USER_AGENT.random}')
    # 可选:绑定本地Chrome用户配置文件(和手动登录环境完全一致)
    # options.add_argument(r'--user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data')
    # options.add_argument('--profile-directory=Default')

    driver = webdriver.Chrome(service=service, options=options)
    # 执行脚本修改navigator.webdriver为undefined
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    
    driver.get('https://www.tiktok.com/')
    # 后续登录逻辑...

2. 修复输入函数的逻辑错误

当前enter_text函数会把输入文本拆分成带无效空格的列表,触发异常检测,修改为模拟真实人类输入的逻辑:

import random

def enter_text(text, field):
    # 遍历每个字符,添加随机延迟
    for char in text:
        field.send_keys(char)
        time.sleep(random.uniform(0.1, 0.3))  # 随机0.1-0.3秒间隔,更贴近人类操作

3. 替换不稳定的绝对XPATH

绝对XPATH极易因页面结构变化失效,改用TikTok官方的data-e2e属性做相对定位:

from selenium.webdriver.common.by import By

# 关闭首页弹窗
driver.find_element(By.CSS_SELECTOR, '[data-e2e="top-close-button"]').click()

# 选择密码登录方式
log_in_method_button = driver.find_element(By.CSS_SELECTOR, '[data-e2e="login-with-password"]')
log_in_method_button.click()

# 切换到用户名/邮箱登录模式
change_method_to_email_or_username = driver.find_element(By.CSS_SELECTOR, '[data-e2e="login-switch-username"]')
change_method_to_email_or_username.click()

# 定位用户名和密码输入框
email_or_username_field = driver.find_element(By.CSS_SELECTOR, '[data-e2e="login-username"]')
password_field = driver.find_element(By.CSS_SELECTOR, '[data-e2e="login-password"]')

4. 用显式等待替代固定sleep

固定sleep会导致等待时间不合理,显式等待能在元素可操作后立即执行,更符合真实操作节奏:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 等待弹窗按钮可点击后再操作
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-e2e="top-close-button"]'))
).click()

额外注意事项

  • 测试间隔至少10分钟以上,避免频繁请求触发TikTok的叠加限制
  • 使用代理IP时,确保IP未被TikTok标记为自动化专用IP
  • 优先绑定本地Chrome用户配置文件,最大程度还原手动登录环境

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 18:35:27