我试图使用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()
我怎么能做这个工作?
click()
不会返回任何东西。因此,您需要删除分配并格式化代码行,以替换:
user = driver.find_element_by_xpath('//span[@title = " {}"]'.format(name)).click()
有:
driver.find_element_by_xpath('//span[@title= "{}"]'.format(name)).click()