无法使用 Python 发送 WhatsApp 消息

Can't send WhatApp message using Python

我正在使用下面的代码示例。我从 https://www.geeksforgeeks.org/whatsapp-using-python/

得到

我对代码进行了必要的修改。比如"changing 'Friend's Name' with an actual friend"、"downloading chromedriver"等

在 运行 代码之后,我注意到它堆叠在行 input_box=wait.until(EC.presence_of_element_located((By.XPATH, inp_xpath)))

stack->mean : 代码一直等到超时,然后给出与超时问题相关的错误

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

# Replace below path with the absolute path 
# to chromedriver in your computer 
driver = webdriver.Chrome('/home/saket/Downloads/chromedriver') 

driver.get("https://web.whatsapp.com/") 
wait = WebDriverWait(driver, 600) 

# Replace 'Friend's Name' with the name of your friend 
# or the name of a group 
target = '"Friend\'s Name"'

# Replace the below string with your own message 
string = "Message sent using Python!!!"

x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located(( 
    By.XPATH, x_arg))) 
group_title.click() 
inp_xpath = '//div[@class="input"][@dir="auto"][@data-tab="1"]'
input_box = wait.until(EC.presence_of_element_located(( 
    By.XPATH, inp_xpath))) 
for i in range(100): 
    input_box.send_keys(string + Keys.ENTER) 
    time.sleep(1) 

我想将 "string" 消息发送到 "target"。任何人都可以帮助我解决我的问题。提前致谢。

根据sally-zeitler的评论,我尝试了一些东西。

  1. 由于 inp_xpath 变量在我的 chrome 浏览器中不一样,我已将其更改为正确的。
  2. 存在与 chrome 浏览器和 chromedriver 相关的兼容性问题,所以我下载了正确的浏览器(如果您使用的是 Chrome 76 版,请下载 ChromeDriver 76.0.3809.126)
  3. 然后,效果很好:)

这是代码示例的最新版本:

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

# Replace below path with the absolute path
# to chromedriver in your computer
driver = webdriver.Chrome('/home/saket/Downloads/chromedriver')

driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 500)
print("#1")
# Replace 'Friend's Name' with the name of your friend
# or the name of a group
#target = '"Friend\'s Name"'
target = '"Your friend name"'
print("#2")
# Replace the below string with your own message
string = '"Message sent using Python!!!"'
print("#3")
x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located((
    By.XPATH, x_arg)))
print("#4")
group_title.click()
print("#5")
inp_xpath = '//div[@class="_3u328 copyable-text selectable-text"]'
input_box = wait.until(EC.presence_of_element_located((
    By.XPATH, inp_xpath)))
print("#6")
for i in range(100):
    print("#7", i)
    input_box.send_keys(string + Keys.ENTER)
    time.sleep(1)