【发布时间】:2020-06-21 18:16:32
【问题描述】:
我正在使用 python 和 selenium 向目标发送消息。
我可以成功打开 whatsapp 网页,但之后我无法打开我要向其发送消息的联系人的收件箱。
这是目前为止的代码。 第一部分很常见,我必须打开网页。它发生没有任何问题。 下一部分是打开联系人以发送收件箱消息。 我尝试了两种不同的方法。他们都没有工作。
通用部分:
#WhatsApp Automation Project
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
#Open WhatsApp web
driver = webdriver.Chrome('D:/Drivers/chromedriver')
driver.get('https://web.whatsapp.com')
#The code should wait sometime for the user to scan the bar code.
sleep(15)
print('Code ended its pause.')
#chose whom to send messaage.
target = 'Name_in_Contacts'
#choose the message to send.
string = 'Guess who learned to autoate WhatsApp using Python.'
方法一:
# what I initially thought of doing
search = driver.find_element_by_class_name("_3FeAD uu8jX")
#class_name is the name of the label of the search box in whatsapp web
#alternatively I had used class name('_3u328 copyable-text selectable-text') , a div class inside the #label class\
#both the class name give the same error.
#check the image of the HTML code.
search.send_keys(target)
报错:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"._3FeAD uu8jX"}
(Session info: chrome=80.0.3987.132)
方法二: 此方法是从 Stack OverFlow 的一种解决方案中复制而来的。
#Stack OverFlow method. Didn't work.
#Open searcch box
search = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME, "_3u328 copyable-text selectable-text")))
#Alternatively, the other class_name of label was also used.
#Both gave the same error.
search.send_keys(target)
报错:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
现在已经一个多月了,我无法解决问题。
请不要关闭已提出的问题。我知道关于这个主题有很多问题,但它们都不适合我,如果你能给我针对我的问题的解决方案,那将是很大的帮助。
提前谢谢你。
【问题讨论】:
-
你找到解决这个问题的方法了吗?
-
@AakashAggarwal 是的。我已在下面发布答案并将其标记为已接受的解决方案。
标签: python selenium selenium-webdriver selenium-chromedriver webautomation