0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
AwsmAsim
  • 3
  • 3
  • 1
    You are trying to find an element by xpath which cannot be found in the page. Acording to the docs if you encounter this exception, you may want to check the following: - Check your selector used in your find_by… - Element may not yet be on the screen at the time of the find operation, (webpage is still loading) see selenium.webdriver.support.wait.WebDriverWait() for how to write a wait wrapper to wait for an element to appear. – gcharbon May 19 '20 at 19:07

3 Answers3

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

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

Edouard Yonga
  • 477
  • 4
  • 12
0

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
Rohit
  • 376
  • 3
  • 9
0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352