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

时间秒杀一切 提交于 2021-02-11 12:27:01

问题


The bot is supposed to send message on whatsApp WEB but unfortunately is stopping and giving error when asked find the user through 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))

The program is stopping exactly after this line, and giving the following error,

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 version:3.8


回答1:


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))

The final line of the traceback output tells you what type of exception was raised along with some relevant information about that exception. The previous lines of the traceback point out the code that resulted in the exception being raised.

here we see that it was unable to locate the path you specified

raison why it stopped




回答2:


I feel the issue is with the element not being in the viewport, THe user you are trying to access must be not in the view, also try to debug the issue manually, by below steps

  1. List item

try to locate the xpath //span[@title='Jaden'] and see if you are able to locate it in the dev tools without scrolling the page down.(If it is able to locate after scrolling the you will have to scroll programmatically using javascript executor.)

  1. Try to see if there is any load time issue and try to implement appropriate explicit wait for the element



回答3:


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:

  • Using 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()
    
  • Using 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()
    
  • Note : You have to add the following imports :

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


来源:https://stackoverflow.com/questions/61898797/how-to-click-on-a-username-within-web-whatsapp-com-using-selenium-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!