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

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

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

白衣非少年 2022-10-06 19:32:10
# 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贡献1795条经验 获得超7个赞

由于同步问题而发生此错误。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()



查看完整回答
反对 回复 2022-10-06
?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

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



查看完整回答
反对 回复 2022-10-06
?
慕神8447489

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

该错误意味着 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)


查看完整回答
反对 回复 2022-10-06
?
Qyouu

TA贡献1786条经验 获得超11个赞

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


try:

    #Insert your scraping action here

    signinButton.click()

except NoSuchElementException:


查看完整回答
反对 回复 2022-10-06
  • 4 回答
  • 0 关注
  • 631 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号