【问题标题】:raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:引发 TimeoutException(消息,屏幕,堆栈跟踪) selenium.common.exceptions.TimeoutException:消息:
【发布时间】: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 行,在执行中

标签: python selenium


【解决方案1】:

您可以处理该异常,

from selenium.common.exceptions import TimeoutException

try:
    driver.get("https://web.whatsapp.com/")
except TimeoutException:
    # You can write retry code here

我希望这会有所帮助。

【讨论】:

  • 文件 "whatsapp.py", line 13, in browser.get(url) NameError: name 'browser' is not defined 在处理上述异常时,又发生了一个异常: Traceback (最近一次调用最后):文件“whatsapp.py”,第 14 行,在 中,除了 TimeoutException:NameError:未定义名称“TimeoutException”
  • 以上是我添加后得到的" try: browser.get(url) except TimeoutException: # You can write retry code here"
  • browser.get(url) NameError: name 'browser' is not defined
  • 请查看更新后的答案。如果您使用单独的函数通过 driver.get() 连接到父亲,那么您可以使该函数在 TimeoutException 中递归。
  • 这里还有一个错误:文件“whatsapp.py”,第 32 行,在 input_box.send_keys(string + Keys.ENTER) NameError: name 'input_box' is not defined
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2015-04-09
相关资源
最近更新 更多