我在使用 Python Selenium 时遇到了这个错误以及网页加载的时间

# Path to the chromedriver program

service = Service('C:\Program Files (x86)\Google\chromedriver.exe')

service.start()


# Driver opens the remote with robinhood website

driver = webdriver.Remote(service.service_url)

driver.get('https://robinhood.com/crypto/BTC')


# We will grab the element id's to log on to Robinhood

# driver.find_element_by_id(“ID”).send_keys(“username”)

# driver.find_element_by_id (“ID”).send_keys(“password”)

# driver.find_element_by_id(“submit”).click()

signinButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk")))


# driver.find_element_by_class_name('_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk')

signinButton.click()


# Closes the driver after timeout

driver.quit()

我基本上是在打开 chrome webdriver 并访问 robinhood 网站,但是我遇到了网页加载问题。为了修复它,我试图使用 WebDriverWait 来停止按钮单击,直到网页加载。


问题是按钮单击不会在 10 秒后执行,而是会引发此错误:


Traceback (most recent call last):

  File "D:/gitRepos/bitmine/runmine.py", line 25, in <module>

    signinButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk")))

  File "D:\Programs Files 2\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until

    raise TimeoutException(message, screen, stacktrace)

selenium.common.exceptions.TimeoutException: Message: 


白衣非少年
浏览 632回答 4
4回答

一只萌萌小番薯

由于同步问题而发生此错误。Yu 可以通过在 selenium 中使用等待来解决您的问题。请参考以下解决方案以避免此类错误:WebDriverWait(driver, 30).until(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EC.element_to_be_clickable((By.XPATH, "//button[@class='_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk']"))).click()注意:请在您的解决方案中添加以下导入from selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWait免费注册按钮部分:wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk']"))).click()wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Sign up for free')]"))).click()

吃鸡游戏

该错误意味着它在 10 秒内没有找到可点击的按钮,并且它超时,抛出 TimeoutException。您需要设置更长的等待时间,或相应地处理 TimeoutException

慕神8447489

该错误意味着 selenium 无法在指定的时间内找到该元素。也不要在类名中使用空格。只需使用点.,否则无论您增加时间,硒都无法找到它。from selenium import webdriverdriver = webdriver.Firefox()driver.get("https://robinhood.com/crypto/BTC")element = driver.find_element_by_class_name(&nbsp; &nbsp; "_3kh8OsNx6QdAbMaoKTi2Yq._1uaripz9PIQ8yApSTs6BKk")print(element)

Qyouu

由于无法在定义的时间段内找到对象,通常会出现错误。我宁愿你设置一个异常错误来捕获它并在它失败时继续查找下一个对象或元素。try:&nbsp; &nbsp; #Insert your scraping action here&nbsp; &nbsp; signinButton.click()except NoSuchElementException:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python