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

Selenium WebDriver用xpath定位WhatsApp发送按钮触发element not interactable异常

报错原因

element not interactable 报错的核心是你定位到的发送按钮当前处于不可交互状态,具体由两个问题导致:

  • Xpath定位不准:你当前使用的全局匹配规则//span[@data-icon="send"]会优先匹配聊天输入框旁的普通消息发送按钮,上传附件后弹出的预览窗口内的发送按钮才是目标元素,前者此时被弹窗遮挡自然无法点击。
  • 硬等待可靠性差:固定sleep2秒的方案受网络波动、附件大小影响很大,很容易出现按钮还没渲染完成就执行点击操作的情况。

修复方案

  1. 调整Xpath规则,增加父级容器限定精准匹配附件预览窗口的发送按钮,优先用更稳定的data-test-id属性定位
  2. 替换硬等待为Selenium显式等待,主动检测元素可点击状态后再执行操作,兼容性更强
  3. 如果仍有点击拦截问题,可改用JS执行点击绕过前端交互限制

修复后代码示例

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

try:
    url = 'https://web.whatsapp.com/send?phone=+91' + number  
    driver.get(url)
    # 等待附件按钮加载完成,超时时间可根据实际情况调整
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@title = "Attach"]')))

    # 上传附件逻辑
    attachment_box = driver.find_element(By.XPATH, '//div[@title = "Attach"]')
    attachment_box.click()
    
    image_box = driver.find_element(By.XPATH, '//input[@accept="image/*,video/mp4,video/3gpp,video/quicktime"]')
    image_box.send_keys(filepath)
    
    # 等待预览窗口发送按钮可点击
    send_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//div[@role="dialog"]//span[@data-test-id="send"]'))
    )
    # 常规点击失效时取消注释下面的JS点击代码
    # driver.execute_script("arguments[0].click();", send_button)
    send_button.click()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 11:06:01