如何使用Selenium和Python在web.whatsapp.com中单击用户名 [英] How to click on a username within web.whatsapp.com using Selenium and Python

查看:136
本文介绍了如何使用Selenium和Python在web.whatsapp.com中单击用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该机器人应该在whatsApp WEB上发送消息,但不幸的是,它被停止并要求通过X-path查找用户时出错.

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版本:3.8

python version:3.8

推荐答案

通过WhatsApp网络向用户发送消息https://web.whatsapp.com/使用,您必须单击为 element_to_be_clickable()生成的 WebDriverWait 用户名,并且可以使用以下

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:

  • 使用 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()

  • 使用 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
    

  • 这篇关于如何使用Selenium和Python在web.whatsapp.com中单击用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆