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

Selenium Python代码无法定位New Request按钮求助

问题:Selenium定位“New Request”按钮失败

目标按钮HTML

<button class="as-main-button definitely-not-a-aff-button" onclick="window.location.href='/FireFlow/SelfService/ChooseTemplate.html?Queue=1';">+ &nbsp;New Request</button>

尝试的代码

try:
    button_class_name = "button.as-main-button.definitely-not-a-aff-button"
    element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, button_class_name)))
    logging.info("New Request element is visible")

    time.sleep(5)
except Exception as e:
    logging.error(f"Failed to create new request : {e}")

报错信息

2023-08-10 01:34:45,850 - ERROR - Failed to create new request : Message: 
Stacktrace:
Backtrace:
    GetHandleVerifier [0x003EA813+48355]
    (No symbol) [0x0037C4B1]
    (No symbol) [0x00285358]
    (No symbol) [0x002B09A5]
    (No symbol) [0x002B0B3B]
    (No symbol) [0x002DE232]
    (No symbol) [0x002CA784]
    (No symbol) [0x002DC922]
    (No symbol) [0x002CA536]
    (No symbol) [0x002A82DC]
    (No symbol) [0x002A93DD]
    GetHandleVerifier [0x0064AABD+2539405]
    GetHandleVerifier [0x0068A78F+2800735]
    GetHandleVerifier [0x0068456C+2775612]
    GetHandleVerifier [0x004751E0+616112]
    (No symbol) [0x00385F8C]
    (No symbol) [0x00382328]
    (No symbol) [0x0038240B]
    (No symbol) [0x00374FF7]
    BaseThreadInitThunk [0x75AA7D59+25]
    RtlInitializeExceptionChain [0x7761B79B+107]
    RtlClearBits [0x7761B71F+191]

补充疑问

从其他页面跳转至当前页面,是否需要先切换driver再定位元素?


解决建议
  • 优化定位方式:你的CSS选择器语法正确,但可以尝试结合按钮文本的XPATH定位,提升精准度:
    # 按文本定位
    element = wait.until(EC.visibility_of_element_located((By.XPATH, "//button[contains(text(), 'New Request')]")))
    # 结合类名和文本的精准定位
    element = wait.until(EC.visibility_of_element_located((By.XPATH, "//button[@class='as-main-button definitely-not-a-aff-button' and contains(text(), 'New Request')]")))
    
  • 更换等待条件:presence_of_element_located仅确保元素存在于DOM,可能未完全显示,换成visibility_of_element_located更适配交互场景:
    element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.as-main-button.definitely-not-a-aff-button")))
    
  • 检查iframe嵌套:如果按钮在iframe内部,必须先切换到iframe才能定位:
    # 定位iframe并切换
    iframe = wait.until(EC.presence_of_element_located((By.ID, "目标iframe的ID")))
    driver.switch_to.frame(iframe)
    # 定位按钮
    element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.as-main-button.definitely-not-a-aff-button")))
    # 操作完成后切回主文档
    driver.switch_to.default_content()
    
  • 页面跳转后的等待:跨页面跳转后,建议先等待页面标志性元素或标题加载完成,避免过早执行定位:
    # 等待页面标题包含指定关键词
    wait.until(EC.title_contains("页面标题关键词"))
    
  • 关于driver切换:普通页面跳转无需切换driver,driver会自动指向当前页面;如果是新窗口/标签页跳转,才需要切换窗口句柄:
    # 获取所有窗口句柄,切换到最新打开的窗口
    handles = driver.window_handles
    driver.switch_to.window(handles[-1])
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 21:07:15