Send whatsapp message to contacts using Python but getting an error: InvalidSelectorException: Message: invalid selector: Unable to locate an element
Question:
I am trying to send whatsapp message to contacts using Python but getting an error:
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)
I have used selenium for this and the code is mentioned below:
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()
How can I make this work ???
Answers:
click()
doesn’t returns anything. So you need to remove the assignment and format the line of code properly replacing:
driver.find_element_by_xpath('//span[@title = "{}"]'.format(name)).click()
with:
driver.find_element_by_xpath('//span[@title= "{}"]'.format(name)).click()
Might be the simple way to send whatsapp message with speak function or automate whatsapp message using python without using selenium
First of all
pip install pywhatkit #it is an module for whatsapp
pip install speechRecognition #it is the module which recognizes what the user speaks
After installing this pip modules import them
import speech_recognition as sr
import pywhatkit
The main syntax for sending whatsapp message is
pywhatkit.sendwhatmsg(phone_no, msg, time_h, time_m, 15)
Secondly define a function like this:
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!")
Now we have successfully created the function. let us call the function
if __name__ == "__main__":
wishme()
while True:
query = TakeCommand().lower() # to take command from user
if 'whatsapp message' in query:
whatsapp_msg()
I am trying to send whatsapp message to contacts using Python but getting an error:
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)
I have used selenium for this and the code is mentioned below:
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()
How can I make this work ???
click()
doesn’t returns anything. So you need to remove the assignment and format the line of code properly replacing:
driver.find_element_by_xpath('//span[@title = "{}"]'.format(name)).click()
with:
driver.find_element_by_xpath('//span[@title= "{}"]'.format(name)).click()
Might be the simple way to send whatsapp message with speak function or automate whatsapp message using python without using selenium
First of all
pip install pywhatkit #it is an module for whatsapp
pip install speechRecognition #it is the module which recognizes what the user speaks
After installing this pip modules import them
import speech_recognition as sr
import pywhatkit
The main syntax for sending whatsapp message is
pywhatkit.sendwhatmsg(phone_no, msg, time_h, time_m, 15)
Secondly define a function like this:
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!")
Now we have successfully created the function. let us call the function
if __name__ == "__main__":
wishme()
while True:
query = TakeCommand().lower() # to take command from user
if 'whatsapp message' in query:
whatsapp_msg()