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

Selenium执行WhatsApp自动脚本时click()失效,消息发送环节异常

解决WhatsApp Web Selenium脚本中发送按钮click()失效的问题

我来帮你排查这个发送按钮点击没反应的问题,这类情况在WhatsApp Web的Selenium自动化场景里挺常见的,主要有这几个核心原因和对应的解决办法:

1. 元素定位器不稳定

你当前用的_1JNuk这个class名是WhatsApp前端动态生成的,很容易随着页面更新而变化,这会直接导致Selenium找不到正确的发送按钮元素。

解决办法:改用更稳定的定位方式,比如基于按钮的功能属性或图标来写XPATH:

  • 比如通过按钮的aria-label属性(中文环境下是“发送”,其他语言可对应调整):
    button = driver.find_element_by_xpath('//button[@aria-label="发送"]')
    
  • 或者通过发送图标的data-icon属性定位(这个不受语言影响,通用性更强):
    button = driver.find_element_by_xpath('//span[@data-icon="send"]/ancestor::button')
    

2. 元素未完全就绪就执行点击

循环里每次发送消息后,发送按钮可能还没完全加载完成,直接调用click()大概率会失败。time.sleep()不是可靠的等待方式,建议用显式等待确保元素处于可点击状态。

解决办法:引入WebDriverWait和expected_conditions来实现智能等待:

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

# 替换循环里的代码
for i in range(count):
    msg_box.send_keys(msg)
    # 等待发送按钮可点击,最多等待5秒
    button = WebDriverWait(driver, 5).until(
        EC.element_to_be_clickable((By.XPATH, '//span[@data-icon="send"]/ancestor::button'))
    )
    button.click()

3. 替代方案:模拟回车发送

如果点击按钮的方式始终有问题,你可以试试直接在输入框按回车发送消息——这是WhatsApp Web原生支持的功能,稳定性比点击按钮更高:

from selenium.webdriver.common.keys import Keys

# 替换循环里的点击逻辑
for i in range(count):
    msg_box.send_keys(msg)
    msg_box.send_keys(Keys.ENTER)

额外优化建议

  • 把开头的time.sleep(10)换成显式等待,等待扫码登录后的聊天列表加载完成,避免因网络慢导致后续操作失败:
    WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.XPATH, '//div[@data-testid="chat-list"]'))
    )
    
  • 同样,别用find_element_by_class_name定位输入框,换成更稳定的XPATH:
    msg_box = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//div[@data-testid="conversation-compose-box-input"]'))
    )
    

修改后的完整示例代码

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

driver = webdriver.Chrome(executable_path="D:/Python/Python/python/chromedriver_win32/chromedriver.exe")
driver.get("https://web.whatsapp.com/")

# 等待聊天列表加载完成(替代time.sleep)
WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.XPATH, '//div[@data-testid="chat-list"]'))
)

name = input('Enter the name of user or group : ')
msg = input('Enter the message : ')
count = int(input('Enter the count : '))

# 定位用户/群组
user = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))
user.click()

# 定位消息输入框
msg_box = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//div[@data-testid="conversation-compose-box-input"]'))
)

for i in range(count):
    msg_box.send_keys(msg)
    # 方式1:用回车发送(推荐)
    msg_box.send_keys(Keys.ENTER)
    # 方式2:用按钮点击(如果想用这个就注释掉上面的回车代码)
    # button = WebDriverWait(driver, 5).until(
    #     EC.element_to_be_clickable((By.XPATH, '//span[@data-icon="send"]/ancestor::button'))
    # )
    # button.click()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 06:32:28