Can';t使用Python发送WhatApp消息

Can';t使用Python发送WhatApp消息,python,whatsapp,Python,Whatsapp,我正在使用下面的代码示例。我从你那里得到的 我对代码做了必要的修改。比如“和真正的朋友更改‘朋友的名字’”、“下载chromedriver”等等 在运行代码之后,我注意到它在input\u box=wait.until(EC.presence\u of_element\u located((By.XPATH,inp\u XPATH))行堆叠 堆栈->平均值:代码等待超时,然后给出与超时问题相关的错误 from selenium import webdriver from selenium.we

我正在使用下面的代码示例。我从你那里得到的

我对代码做了必要的修改。比如“和真正的朋友更改‘朋友的名字’”、“下载chromedriver”等等

在运行代码之后,我注意到它在
input\u box=wait.until(EC.presence\u of_element\u located((By.XPATH,inp\u XPATH))
行堆叠

堆栈->平均值:代码等待超时,然后给出与超时问题相关的错误

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) 
我希望将“字符串”消息发送到“目标”。有什么能帮我解决我的问题吗。提前谢谢。

在您的评论之后,我尝试了一些方法

  • 由于
    inp\uxpath
    变量在我的chrome浏览器中不相同,我将其更改为正确的变量
  • chrome浏览器和chromedriver存在兼容性问题,因此我下载了正确的版本(如果您使用的是chrome 76版,请下载chromedriver 76.0.3809.126)
  • 然后,它工作得很好:)
  • 以下是代码示例的最新版本:

    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)
    
    在发表评论之后,我尝试了一些事情

  • 由于
    inp\uxpath
    变量在我的chrome浏览器中不相同,我将其更改为正确的变量
  • chrome浏览器和chromedriver存在兼容性问题,因此我下载了正确的版本(如果您使用的是chrome 76版,请下载chromedriver 76.0.3809.126)
  • 然后,它工作得很好:)
  • 以下是代码示例的最新版本:

    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)
    

    您的文章中有一条评论说whatapp更改了类以便您可以尝试:inp_xpath='//div[@dir=“ltr”][@data tab=“1”][@spellcheck=“true”]“无论如何,你应该试着找出xpath的来源/为什么会这样构建,并检查你文章中的评论部分,因为有很多人都有类似的问题——一个人说这很好:我理解它有什么问题。现在,我正在尝试解决这个问题。你文章中的一条评论说whatapp更改了类,因此你可以尝试:inp_xpath='//div[@dir=“ltr”][@data tab=“1”][@spellcheck=“true”]“无论如何,你应该试着找出xpath的来源/为什么会这样构建,并检查你文章中的评论部分,因为有很多人都有类似的问题——一个人说这很好:我理解它有什么问题。现在,我正在努力解决这个问题。