【问题标题】:WhatsApp Web Automation with Python Selenium (unable to locate element)带有 Python Selenium 的 WhatsApp Web 自动化(无法定位元素)
【发布时间】:2020-06-21 18:16:32
【问题描述】:

我正在使用 python 和 selenium 向目标发送消息。

我可以成功打开 whatsapp 网页,但之后我无法打开我要向其发送消息的联系人的收件箱。

这是目前为止的代码。 第一部分很常见,我必须打开网页。它发生没有任何问题。 下一部分是打开联系人以发送收件箱消息。 我尝试了两种不同的方法。他们都没有工作。

通用部分:

#WhatsApp Automation Project
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC 
from time import sleep

#Open WhatsApp web
driver =  webdriver.Chrome('D:/Drivers/chromedriver')
driver.get('https://web.whatsapp.com')


#The code should wait sometime for the user to scan the bar code.
sleep(15)
print('Code ended its pause.')

#chose whom to send messaage.
target = 'Name_in_Contacts'

#choose the message to send.
string = 'Guess who learned to autoate WhatsApp using Python.'

方法一:

# what I initially thought of doing

search  = driver.find_element_by_class_name("_3FeAD uu8jX")
#class_name is the name of the label of the search box in whatsapp web
#alternatively I had used class name('_3u328 copyable-text selectable-text') , a div class inside the  #label class\
#both the class name give the same error.
#check the image of the HTML code.
search.send_keys(target)

报错:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"._3FeAD uu8jX"}
  (Session info: chrome=80.0.3987.132)

方法二: 此方法是从 Stack OverFlow 的一种解决方案中复制而来的。

#Stack OverFlow method. Didn't work.

#Open searcch box
search = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME, "_3u328 copyable-text selectable-text")))
#Alternatively, the other class_name of label was also used.
#Both gave the same error.
search.send_keys(target)

报错:

raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

现在已经一个多月了,我无法解决问题。

请不要关闭已提出的问题。我知道关于这个主题有很多问题,但它们都不适合我,如果你能给我针对我的问题的解决方案,那将是很大的帮助。

提前谢谢你。

【问题讨论】:

  • 你找到解决这个问题的方法了吗?
  • @AakashAggarwal 是的。我已在下面发布答案并将其标记为已接受的解决方案。

标签: python selenium selenium-webdriver selenium-chromedriver webautomation


【解决方案1】:

终于,我得到了答案。

我意识到打开搜索框毫无意义。 在搜索框中输入姓名只会将姓名显示在顶部,不能选择任何联系人。

我们应该从屏幕的左中角找到联系人的姓名。

用于该用途:

user = driver.find_element_by_xpath("//span[@title='{}']".format(name))
user.click()

它会打开收件箱。

完成项目的链接在这里: https://github.com/AshTiwari/WhatsApp-Automation-using-selenium-and-Python 请给我一颗星,因为它真的会鼓励我。

【讨论】:

    【解决方案2】:

    希望这会对你有所帮助。

    element = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[text()='Search or start new chat']/preceding-sibling::button]");
    element.click();
    //again wait  and then use sendkeys to select or search contact
    

    【讨论】:

    • 首先,我照原样复制了整个代码。由于第一行末尾缺少两个右括号)),它给出了语法错误。
    • 后来我添加了两个括号,然后它给了我错误:NameError: name wait is not defined。于是我把它改成了WebDriverWait.until(...)
    • 然后它给出了一个错误until() missing 1 required positional argument: 'method'。所以我把它改成了WebDriverWait(driver,5).until(...)
    • 代码终于变成了search = WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH, "//*[text()='Search or start new chat']/preceding-sibling::button]")))。它给出了错误selenium.common.exceptions.TimeoutException: Message:
    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 2015-09-19
    • 2020-12-29
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多