为了账号安全,请及时绑定邮箱和手机立即绑定

我在使用 Python Selenium 时遇到网页加载时间错误

我在使用 Python Selenium 时遇到网页加载时间错误

www说 2023-12-11 16:01:28
# Path to the chromedriver programservice = Service('C:\Program Files (x86)\Google\chromedriver.exe')service.start()# Driver opens the remote with robinhood websitedriver = 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 timeoutdriver.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: 
查看完整描述

4 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

此错误是由于同步问题而发生的。Yu可以通过使用selenium中的等待来解决您的问题。请参考以下解决方案以避免此类错误:


WebDriverWait(driver, 30).until(

                EC.element_to_be_clickable((By.XPATH, "//button[@class='_3kh8OsNx6QdAbMaoKTi2Yq _1uaripz9PIQ8yApSTs6BKk']"))).click()

注意:请将以下导入添加到您的解决方案中


from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

from 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()



查看完整回答
反对 回复 2023-12-11
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

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



查看完整回答
反对 回复 2023-12-11
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

该错误通常是由于无法在定义的时间段内找到该对象而引发的。我宁愿你设置一个异常错误来捕获它,并在失败时继续查找下一个对象或元素。


try:

    #Insert your scraping action here

    signinButton.click()

except NoSuchElementException:


查看完整回答
反对 回复 2023-12-11
?
Helenr

TA贡献1780条经验 获得超3个赞

该错误意味着 selenium 无法在指定的时间内找到该元素。


也不要在类名中使用空格。只需使用点.,否则无论您增加时间,硒都将无法找到它。


from selenium import webdriver


driver = webdriver.Firefox()


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


element = driver.find_element_by_class_name(

    "_3kh8OsNx6QdAbMaoKTi2Yq._1uaripz9PIQ8yApSTs6BKk")


print(element)


查看完整回答
反对 回复 2023-12-11
  • 4 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信