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

如何避免Selenium被拦截?登录prenotami.esteri.it遇跳转问题求助

解决Selenium登录prenotami.esteri.it被拦截的思路

1. 全面伪装浏览器指纹

仅修改navigator.webdriver不足以绕过检测,网站通常会校验多个浏览器特征,可通过注入专业伪装脚本强化:

  • 下载stealth.min.js脚本(专门针对Selenium自动化特征的伪装工具),在浏览器加载页面前注入:
    with open('stealth.min.js') as f:
        stealth_script = f.read()
    # 确保每个新页面加载时都执行该脚本
    driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': stealth_script})
    
  • 放弃随机生成UA,直接复制当前正常使用的Chrome/Firefox的UA字符串,随机UA大概率会被标记为异常请求。

2. 优化交互逻辑,贴近真人行为

  • 输入字符的间隔不要固定为0.1秒,改为随机波动范围(比如0.05-0.2秒),同时偶尔加入鼠标移动、点击页面空白区域的操作:
    import random
    # 邮箱输入示例
    for c in correo:
        time.sleep(random.uniform(0.05, 0.2))
        email.send_keys(c)
    # 模拟鼠标移动到输入框
    from selenium.webdriver.common.action_chains import ActionChains
    actions = ActionChains(driver)
    actions.move_to_element(email).click().perform()
    
  • 替换绝对XPATH定位按钮,改用相对CSS选择器,避免页面结构变化导致定位失效,同时减少自动化特征暴露:
    boton_avanti = driver.find_element(By.CSS_SELECTOR, "section.form-section button[type='submit']")
    
  • 用ActionChains模拟滚动,替代直接执行scrollTo脚本:
    actions.move_to_element(password).perform()  # 滚动到密码框位置
    

3. 适配网站HTTP头与Cookie策略

  • 通过Selenium Wire设置符合真人请求的HTTP头,补充Referer、Accept-Language等字段:
    driver.header_overrides = {
        'Referer': 'https://prenotami.esteri.it/',
        'Accept-Language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7'
    }
    
  • 首次手动完成网站的人机验证后,保存Cookie复用:
    import pickle
    # 登录成功后保存Cookie
    pickle.dump(driver.get_cookies(), open("prenotami_cookies.pkl", "wb"))
    # 后续启动浏览器时加载Cookie
    driver.get('https://prenotami.esteri.it')
    cookies = pickle.load(open("prenotami_cookies.pkl", "rb"))
    for cookie in cookies:
        driver.add_cookie(cookie)
    driver.refresh()
    

4. 复用真实浏览器环境

  • 连接已手动打开的正常浏览器,完全绕过自动化环境检测:
    1. 手动启动Chrome时添加参数:chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\YourChromeProfile"
    2. 脚本中连接该浏览器:
    options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    driver = webdriver.Chrome(options=options)
    

内容的提问来源于stack exchange,提问作者Diego K.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 19:45:02