我正在尝试使用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)
我为此使用了硒,下面提到了代码:
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()
我如何进行这项工作?
答案 0 :(得分:0)
click()
不返回任何内容。因此,您需要删除分配并格式化正确替换的代码行:
user = driver.find_element_by_xpath('//span[@title = " {}"]'.format(name)).click()
具有:
driver.find_element_by_xpath('//span[@title= "{}"]'.format(name)).click()
答案 1 :(得分:0)
可能是使用语音功能发送 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()