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

Python Selenium无法重定向至目标URL问题求助

问题描述

学习项目中做网站自动化,执行操作后无法重定向到目标URL,页面持续加载后报错。具体流程:

  1. 访问example.com
  2. 输入记录定位符QMUEJP、邮箱xyzz2@gamil.com并点击提交
  3. 预期跳转到详情页(如example.com/dfuefh343643746ghbfb@/details/xyz),但代码能正确填写提交表单,却一直加载不跳转,最终超时报错。

代码片段:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time


def main():
    s = Service('D:/Chrome_WebDriver/chromedriver.exe')
    driver = webdriver.Chrome(service=s)
    driver.maximize_window()
    wait = WebDriverWait(driver, 10)
    driver.get("https://example.com")
    time.sleep(5)
    input_f1 = driver.find_element(By.NAME, "RetrieveDetails.RecordLocator").send_keys("QMUEJP")
    input_f2 = driver.find_element(By.NAME, "polymorphicField").send_keys("xyzz2@gamil.com")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[normalize-space()="Submit"]'))).click()
    
    wait.until(EC.url_changes("https://example.com"))


if __name__=='__main__':
    main()

报错信息:

DevTools listening on ws://127.0.0.1:54778/devtools/browser/750e264e-9107-4a6a-9e2e-aa6dd456fae0
[18308:8628:0818/212846.227:ERROR:device_event_log_impl.cc(222)] [21:28:46.227] USB: usb_service_win.cc:415 Could not read device interface GUIDs: The system cannot find the file specified. (0x2)
Traceback (most recent call last):
  File "d:\Python-Code\Script-Selenium\script2.py", line 26, in <module>
    main()
  File "d:\Python-Code\Script-Selenium\script2.py", line 21, in main
    wait.until(EC.url_changes("https://example.com"))        
  File "d:\Python-Code\Python API Code\Python_api\API\lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until     
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

注:最初添加wait.until(EC.url_changes("https://example.com"))后代码可正常运行,关闭后重新运行就出现该错误。Chrome版本116.0.5845.97,ChromeDriver版本116.0.5845.96(版本匹配),换EdgeDriver也出现相同错误。

排查思路与解决方案

可能原因及对应解决办法

  • 页面跳转逻辑限制:部分网站提交表单后会先异步处理请求,或跳转前有中间加载环节,EC.url_changes仅检测URL变化,若页面因请求阻塞未触发跳转,会直接超时。

    • 解决:替换等待条件,改为等待目标页面的特征元素出现(比如详情页的唯一标识元素):
      # 替换原url_changes等待,假设详情页存在id为"detail-content"的元素
      wait.until(EC.presence_of_element_located((By.ID, "detail-content")))
      
    • 或等待URL包含目标路径:
      wait.until(EC.url_contains("/details/"))
      
  • 表单提交未有效触发:按钮点击可能因页面渲染问题未被正确识别(比如按钮被浮层覆盖、点击坐标偏差)。

    • 解决:改用回车键提交表单,替代按钮点击:
      # 输入第二个字段后直接按回车提交
      input_f2 = driver.find_element(By.NAME, "polymorphicField")
      input_f2.send_keys("xyzz2@gamil.com")
      input_f2.send_keys(Keys.ENTER)
      
    • 或用JavaScript强制触发点击:
      submit_btn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//button[normalize-space()="Submit"]')))
      driver.execute_script("arguments[0].click();", submit_btn)
      
  • 浏览器会话/缓存异常:首次运行后缓存的会话数据导致二次运行时请求状态异常。

    • 解决:启动浏览器时使用无痕模式,避免缓存干扰:
      options = webdriver.ChromeOptions()
      options.add_argument("--incognito")
      driver = webdriver.Chrome(service=s, options=options)
      
  • 等待时间不足:网站服务器响应慢,原10秒等待时长不足以完成跳转。

    • 解决:延长等待时间,同时设置页面加载超时:
      wait = WebDriverWait(driver, 30)
      driver.set_page_load_timeout(30)
      
  • 驱动版本不完全匹配:当前Chrome版本为116.0.5845.97,ChromeDriver用的是116.0.5845.96,建议更新到完全一致的驱动版本,避免兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 08:08:11