gpt4 book ai didi

python - 无法使用Python发送WhatsApp消息

转载 作者:太空宇宙 更新时间:2023-11-03 20:29:45 24 4
gpt4 key购买 nike

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

我对代码进行了必要的更改。例如“将‘ friend 的名字’更改为真正的 friend ”、“下载 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)

我希望将“字符串”消息发送到“目标”。任何人都可以帮助我解决我的问题。提前致谢。

最佳答案

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)

关于python - 无法使用Python发送WhatsApp消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57582981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com