Send whatsapp message to contacts using Python but getting an error: InvalidSelectorException: Message: invalid selector: Unable to locate an element

前提是你 提交于 2021-02-04 08:25:07

问题


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 ???


回答1:


click() doesn't returns anything. So you need to remove the assignment and format the line of code properly replacing:

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

with:

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


来源:https://stackoverflow.com/questions/55919620/send-whatsapp-message-to-contacts-using-python-but-getting-an-error-invalidsele

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!