简体   繁体   English

无法使用 chrome 驱动程序 Python 发送 WhatsApp 消息错误:NoSuchElementException

[英]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:我正在使用 Selenium 的 Chrome 驱动程序向某些人发送 WhatsApp 附件:这是我的代码:

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:\Coding\Python Scripts\Dr Nikhil Prescription App\Prescription Generator\WA-testing.py", line 15, in <module>
    attachbutt=driver.find_element_by_xpath('//span[@data-icon="clip"]')
  File "C:\Users\Deven\AppData\Local\Programs\Python\Python39\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\Deven\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Deven\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Deven\AppData\Local\Programs\Python\Python39\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[@data-icon="clip"]"}
  (Session info: chrome=90.0.4430.212)

PS D:\Coding\Python Scripts\Dr Nikhil Prescription App\Prescription 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!谢谢你!

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"]')))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 向联系人发送 whatsapp 消息但收到错误:InvalidSelectorException:消息:无效选择器:无法定位元素 - Send whatsapp message to contacts using Python but getting an error: InvalidSelectorException: Message: invalid selector: Unable to locate an element selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法使用带有Python的Selenium ChromeDriver Chrome找到元素 - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element using Selenium ChromeDriver Chrome with Python NoSuchElementException:消息:没有这样的元素:无法定位元素错误使用 Selenium 和 Python 使用类名搜索元素 - NoSuchElementException: Message: no such element: Unable to locate element error searching for element using classname using Selenium and Python NoSuchElementException:消息:没有这样的元素:使用 Selenium 和 Python 刮擦前 20 个支架时无法定位元素错误 - NoSuchElementException: Message: no such element: Unable to locate element error while scraping the top 20 holder using Selenium and Python selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法通过 Python 使用 Selenium 定位元素错误 - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element error using Selenium through Python NoSuchElementException:消息:没有这样的元素:无法找到元素错误使用 Selenium 和 Python 填充用户名 - NoSuchElementException: Message: no such element: Unable to locate element error filling user name using Selenium and Python 使用python在whatsapp中发送自动消息时出错 - Error in texting automated message in whatsapp using python 使用 selenium 和 python3.8 加入 whatsapp 组时出现 NoSuchElementException - NoSuchElementException while joining whatsapp group using selenium and python3.8 无法使用python selenium错误登录:NoSuchElementException错误 - Unable to login with python selenium error:NoSuchElementException error 尝试使用 python selenium 在 whatsapp 上发送消息 - trying to send message on whatsapp with python selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM