This is my first post here! I am using Selenium’s Chrome Driver to send WhatsApp attachment to some people. Here is my code:
JavaScript
x
31
31
1
from selenium import webdriver
2
import os
3
from time import sleep
4
5
link="https://wa.me/91xxxxxxxxxx"
6
phnno="91xxxxxxxxxx"
7
8
driver=webdriver.Chrome(executable_path=f"{os.getcwd()}\chromedriver.exe")
9
#driver.get(link)
10
#button=driver.find_element_by_xpath('//a[@title="Share on WhatsApp"]')
11
#button.click()
12
13
driver.get(f"https://web.whatsapp.com/send?phone={phnno}&text&app_absent=0")
14
#This above line opens up the sender window in whatsapp
15
16
attachbutt=driver.find_element_by_xpath('//span[@data-icon="clip"]') #This is line 15
17
#The above line is the one that is giving me the error
18
attachbutt.click()
19
20
sleep(10)
21
22
forpdf=driver.find_element_by_xpath('//input[@accept="*"]')
23
24
path="C:\Users\Deven\Desktop\test_file.pdf"
25
26
forpdf.send_keys(path) #Attaches the file
27
sleep(5)
28
29
sendbutt=driver.find_element_by_xpath('//span[@data-icon="send"]')
30
sendbutt.click()
31
ERROR:
JavaScript
1
17
17
1
DevTools listening on ws://127.0.0.1:56230/devtools/browser/1a8a2adb-37ee-4b0c-bedc-5cfb58559c24
2
Traceback (most recent call last):
3
File "d:CodingPython ScriptsDr Nikhil Prescription AppPrescription GeneratorWA-testing.py", line 15, in <module>
4
attachbutt=driver.find_element_by_xpath('//span[@data-icon="clip"]')
5
File "C:UsersDevenAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 394, in find_element_by_xpath
6
return self.find_element(by=By.XPATH, value=xpath)
7
File "C:UsersDevenAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 976, in find_element
8
return self.execute(Command.FIND_ELEMENT, {
9
File "C:UsersDevenAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
10
self.error_handler.check_response(response)
11
File "C:UsersDevenAppDataLocalProgramsPythonPython39libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
12
raise exception_class(message, screen, stacktrace)
13
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@data-icon="clip"]"}
14
(Session info: chrome=90.0.4430.212)
15
16
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)
17
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
JavaScript
1
2
1
sleep(5)
2
before
JavaScript
1
2
1
attachbutt=driver.find_element_by_xpath('//span[@data-icon="clip"]')
2
However, it’s much better to use expected conditions there. Like this:
JavaScript
1
6
1
from selenium.webdriver.support.ui import WebDriverWait
2
from selenium.webdriver.support import expected_conditions as EC
3
4
attachbutt = WebDriverWait(driver, 20).until(
5
EC.element_to_be_clickable((By.XPATH, '//span[@data-icon="clip"]')))
6