selenium 使用Python向联系人发送whatsapp消息,但收到错误:无效选择器异常:消息:无效的选择器:找不到元素

sbdsn5lh  于 2023-02-08  发布在  Python
关注(0)|答案(2)|浏览(177)

我尝试使用Python向联系人发送whatsapp消息,但收到错误:InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //span[@title = "Me Postpaid"]"} (Session info: chrome=73.0.3683.103) (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 6.1.7601 SP1 x86_64)
我已经使用 selenium 为这和代码如下所述:

from selenium import webdriver

driver = webdriver.Chrome('C:/Users/....../chromedriver_win32/chromedriver.exe') 
driver.get('https://web.whatsapp.com/')

name = input('Enter the name of person or group you want to message: ')
msg = input('Enter your Message: ')
count = int(input('Enter how many times you want to send this message: '))

input('Enter any key after scanning QR code')

user = driver.find_element_by_xpath('//span[@title = "        {}"]'.format(name)).click()
#user.click()

msg_box = driver.find_element_by_class_name('_1Plpp')

for i in range(count):

    msg_box.send_keys(msg)
    button = driver.find_element_by_class_name('_35EW6')
    button.click()

我该怎么做呢?

6ie5vjzr

6ie5vjzr1#

click()不返回任何值,因此需要删除赋值语句并正确格式化代码行,替换:

driver.find_element_by_xpath('//span[@title = "{}"]'.format(name)).click()

与:

driver.find_element_by_xpath('//span[@title= "{}"]'.format(name)).click()
ctzwtxfj

ctzwtxfj2#

可能是使用speak函数发送whatsapp消息或使用python自动发送whatsapp消息而不使用selenium的简单方法
首先

pip install pywhatkit #it is an module for whatsapp
pip install speechRecognition #it is the module which recognizes what the user speaks

安装此pip模块后导入它们

import speech_recognition as sr 
import pywhatkit

发送whatsapp消息的主要语法是

pywhatkit.sendwhatmsg(phone_no, msg, time_h, time_m, 15)

其次,定义一个函数,如下所示:

engine = pyttsx3.init('sapi5') # to take voice & search in google
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[1].id) # [0]for male voice
def wishme():
    speak("Welcome  Sir!")

def TakeCommand():
   r=sr.Recognizer()
   with sr.Microphone() as source:
      print("Listening....")
      r.pause_threshold = 0.7
      audio = r.listen(source)

   try:
     print("Recognizing....")
     query = r.recognize_google(audio, language='en-US')
     print(query)

   except  Exception as e:
     print(e)
     print("Say that again please")
     return "None"
   return query

def whatsapp_msg():
    speak("Can you please enter phone number of the person to whom you want to send message?")
    phone_no = input("Enter phone number: +91 ")
    phone_no = "+91" + str(phone_no)
    print(phone_no)

    speak("What message do you want to send?")
    msg = TakeCommand()
    print(msg)

    speak("When you want to send message (Now or Later)")
    print("When you want to send message (Now / Later)")

    msg_send_time = TakeCommand()

    if msg_send_time == "now":
         pywhatkit.sendwhatmsg_instantly(phone_no, msg, 15)
    else:
        speak("Enter the time when you want to send the message")
        speak("First Enter the time in hour")
        time_h = int(input("First Enter the time in hour: "))
        speak("Now Enter the time in minutes")
        time_m = int(input("Now Enter the time in minutes: "))

        pywhatkit.sendwhatmsg(phone_no, msg, time_h, time_m, 15)

    speak("Sending the message")
    print("Successfully Sent!")
    speak("Successfully Sent!")

现在我们已经成功地创建了函数。让我们调用函数

if __name__ == "__main__":
    wishme()
    while True:
        query = TakeCommand().lower() # to take command from user
        if 'whatsapp message' in query:
                whatsapp_msg()

相关问题