我在使用 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: 


www说
浏览 71回答 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

繁花不似锦

该错误通常是由于无法在定义的时间段内找到该对象而引发的。我宁愿你设置一个异常错误来捕获它,并在失败时继续查找下一个对象或元素。try:&nbsp; &nbsp; #Insert your scraping action here&nbsp; &nbsp; signinButton.click()except NoSuchElementException:

Helenr

该错误意味着 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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5