Skip to content
Advertisement

Unable to send WhatsApp message using chrome driver Python Error: NoSuchElementException

This is my first post here! I am using Selenium’s Chrome Driver to send WhatsApp attachment to some people. Here is my code:

from selenium import webdriver
import os
from time import sleep

link="https://wa.me/91xxxxxxxxxx"
phnno="91xxxxxxxxxx"

driver=webdriver.Chrome(executable_path=f"{os.getcwd()}\chromedriver.exe")
#driver.get(link)
#button=driver.find_element_by_xpath('//a[@title="Share on WhatsApp"]')
#button.click()

driver.get(f"https://web.whatsapp.com/send?phone={phnno}&text&app_absent=0") 
#This above line opens up the sender window in whatsapp

attachbutt=driver.find_element_by_xpath('//span[@data-icon="clip"]') #This is line 15
#The above line is the one that is giving me the error
attachbutt.click()

sleep(10)

forpdf=driver.find_element_by_xpath('//input[@accept="*"]')

path="C:\Users\Deven\Desktop\test_file.pdf"

forpdf.send_keys(path) #Attaches the file
sleep(5)

sendbutt=driver.find_element_by_xpath('//span[@data-icon="send"]')
sendbutt.click()

ERROR:

DevTools listening on ws://127.0.0.1:56230/devtools/browser/1a8a2adb-37ee-4b0c-bedc-5cfb58559c24
Traceback (most recent call last):
  File "d:CodingPython ScriptsDr Nikhil Prescription AppPrescription GeneratorWA-testing.py", line 15, in <module>
    attachbutt=driver.find_element_by_xpath('//span[@data-icon="clip"]')
  File "C:UsersDevenAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:UsersDevenAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:UsersDevenAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:UsersDevenAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremoteerrorhandler.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[@data-icon="clip"]"}
  (Session info: chrome=90.0.4430.212)

PS D:CodingPython ScriptsDr Nikhil Prescription AppPrescription Generator> [16176:15752:0523/212201.236:ERROR:device_event_log_impl.cc(214)] [21:22:01.236] USB: usb_device_handle_win.cc:1054 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

It says it is unable to locate the element but I have bene very careful in inspecting the website and then writing the code. Still I wonder why it does not work. Can anyone please help me with what is it that I am doing wrnong? Thank You!

Advertisement

Answer

Looks like you are missing a wait / delay before clicking that element.
The simplest solution is to put

sleep(5)

before

attachbutt=driver.find_element_by_xpath('//span[@data-icon="clip"]')

However, it’s much better to use expected conditions there. Like this:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

attachbutt = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//span[@data-icon="clip"]')))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement