繁体   English   中英

如何在 web.whatsapp.com 中单击用户名,使用 Selenium 和 ZA7F5F35423B9274111Z363

[英]How to click on a username within web.whatsapp.com using Selenium and Python

该机器人应该在 whatsApp WEB 上发送消息,但不幸的是,当被要求通过 X-path 找到用户时,它正在停止并给出错误。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Chrome(executable_path="C:\drivers\chromedriver.exe")
driver.get("https://web.whatsapp.com/")
time.sleep(5)

name= input("Enter name")
input("Enter anything after scanning")

time.sleep(2)

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

程序正好在这一行之后停止,并给出以下错误,

Traceback (most recent call last):
  File "C:/Users/myName/PycharmProjects/firstpro/whatsAppBot.py", line 17, in <module>
    user=driver.find_element_by_xpath("//span[@title='{}']".format(name))
  File "C:\Users\myName\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\myName\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\myName\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\myName\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@title='Jaden']"}
  (Session info: chrome=81.0.4044.138)


Process finished with exit code 1

python版本:3.8

Traceback (most recent call last):
  File "C:/Users/myName/PycharmProjects/firstpro/whatsAppBot.py", line 17, in <module>
    user=driver.find_element_by_xpath("//span[@title='{}']".format(name))

回溯 output 的最后一行告诉您引发了哪种类型的异常以及有关该异常的一些相关信息。 回溯的前几行指出了导致引发异常的代码。

在这里我们看到它无法找到您指定的路径

停止的原因

我觉得问题在于元素不在视口中,您尝试访问的用户必须不在视图中,也尝试通过以下步骤手动调试问题

  1. 项目清单

尝试找到 xpath //span[@title='Jaden']并查看是否能够在不向下滚动页面的情况下在开发工具中找到它。(如果它能够在滚动后找到,则必须滚动以编程方式使用 javascript 执行器。)

  1. 尝试查看是否存在任何加载时间问题并尝试执行适当的显式等待元素

To send a message to a user through WhatsApp web https://web.whatsapp.com/ using Selenium you have to click on the username inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • 使用CSS_SELECTOR

     driver.get("https://web.whatsapp.com/") name= input("Enter name:") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[role='option'] span[title='{}']".format(name)))).click()
  • 使用XPATH

     driver.get("https://web.whatsapp.com/") name= input("Enter name:") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@role='option']//span[@title='{}']".format(name)))).click()
  • 注意:您必须添加以下导入:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM