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

问题描述 投票:0回答:2

该机器人应该在whatsApp WEB上发送消息,但不幸的是,它停止运行并在被问到通过X路径查找用户时给出错误。

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

python selenium xpath css-selectors webdriverwait
2个回答
0
投票
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))

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

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

为什么停下来的理由


0
投票

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

  1. 列表项

尝试找到xpath //span[@title='Jaden'],看看是否能够在dev工具中找到它而无需向下滚动页面。(如果能够在滚动后找到它,则必须使用javascript执行程序以编程方式滚动。)

  1. 尝试查看是否存在任何加载时间问题,并尝试实施适当的显式等待元素
© www.soinside.com 2019 - 2024. All rights reserved.