【发布时间】:2017-05-28 00:40:33
【问题描述】:
我正在尝试按照以下代码通过 python 使用 WhatsApp,但收到以下错误:raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
代码有问题,还是我在这里遗漏了什么? 有人可以帮我解决这个问题吗?如何在 WhatsApp 中使用 python 脚本发送自动回复。
感谢您的帮助。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
# Replace below path with the absolute path
# to chromedriver in your computer
driver = webdriver.Chrome('/home/saket/Downloads/chromedriver')
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)
# Replace 'Friend's Name' with the name of your friend
# or the name of a group
target = '"Friend\'s Name"'
# Replace the below string with your own message
string = "Message sent using Python!!!"
x_arg = '//span[contains(@title,' + target + ')]'
group_title = wait.until(EC.presence_of_element_located((
By.XPATH, x_arg)))
group_title.click()
inp_xpath = '//div[@class="input"][@dir="auto"][@data-tab="1"]'
input_box = wait.until(EC.presence_of_element_located((
By.XPATH, inp_xpath)))
for i in range(100):
input_box.send_keys(string + Keys.ENTER)
time.sleep(1)
【问题讨论】:
-
我只看到一个具有上述属性的元素 (
class="input", dir="auto", data-tab="1"),它不是div,而是input...你能试试inp_xpath = '//input[@class="input"][@dir="auto"][@data-tab="1"]'吗?还要注意复合类名是"input input-search",但不仅仅是"input",所以你应该使用[contains(@class, "input")]或[@class="input-search"] -
这里是上述更新后的输出: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotVisibleException: Message: element not visible (Session info: chrome=58.0.3029.110) (驱动信息:chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platf
-
你在哪一行得到它?
-
回溯(最近一次调用最后):文件“whatsapp.py”,第 25 行,在
group_title.click() 文件“C:\Users\Desktop\ML\WinPython-64bit- 3.4.4.2\python-3.4.4.amd64\lib\site-packages\selenium\webdriver\remote\webelement.py",第 77 行,点击 -
self._execute(Command.CLICK_ELEMENT) 文件“C:\Users\Desktop\ML\WinPython-64bit-3.4.4.2\python-3.4.4.amd64\lib\site-packages\selenium \webdriver\remote\webelement.py”,第 493 行,在 _execute 中返回 self._parent.execute(command, params) 文件“C:\Users\Desktop\ML\WinPython-64bit-3.4.4.2\python-3.4.4 .amd64\lib\site-packages\selenium\webdriver\remote\webdriver.py",第 252 行,在执行中