简体   繁体   English

将chromedriver与Selenium结合使用时,如何更正超时错误?

[英]While using the chromedriver with Selenium, how do I correct a timeout error?

I may be missing something simple here but I have tried a lot already without any luck. 我可能在这里缺少一些简单的东西,但是我已经尝试了很多,没有任何运气。 I'm new to selenium and I cannot correct the following issue. 我是硒新手,无法解决以下问题。 When navigating to a web-page using get() I continually get a timeout message. 使用get()导航到网页时,我不断收到超时消息。 The page loads properly but after everything on the page loads (i assume it may have something to do with how long it takes to load due to the ads loading) I get this error. 页面加载正确,但是页面上的所有内容加载完毕后(我认为这可能与广告加载导致的加载时间有关),我收到了此错误消息。

selenium.common.exceptions.TimeoutException: Message: timeout (Session info: chrome=65.0.3325.181) (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64) selenium.common.exceptions.TimeoutException:消息:超时(会话信息:chrome = 65.0.3325.181)(驱动程序信息:chromedriver = 2.36.540470(e522d04694c7ebea4ba8821272dbef4f9b818c91),platform = Windows NT 10.0.16299 x86_64)

I have tried the following; 我已经尝试了以下方法; moving chromedriver location, trying older versions of selenium, waits, implicitly waits, time.sleep and others. 移动chromedriver位置,尝试使用较旧版本的硒,进行等待,隐式等待,time.sleep等。 Any input would be great as this seems like something simple and I would like to get it fixed soon. 任何输入都会很棒,因为这似乎很简单,我希望尽快解决。

The code in question: 有问题的代码:

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome("Path\To\chromedriver.exe")
driver.set_page_load_timeout(10)

driver.get("https://www.website.com")
driver.find_element_by_name("name").send_keys("com")
driver.find_element_by_name("word").send_keys("pw")
driver.find_element_by_id("idItem").click()

driver.find_element_by_name("word").send_keys(Keys.ENTER)

#driver.implicitly_wait(10)
driver.get("https://www.website2.com")
--------------Error here, never gets past this point------------
time.sleep(10)
driver.close()

As per your question while navigating to a web-page using get() apparently it may appear that the page got loaded properly but factually the JavaScripts and Ajax Calls under the hood might not have completed and the Web Client may not have achieved 'document.readyState' is equal to "complete" as well. 根据您在使用get()导航至网页时遇到的问题,显然页面似乎已正确加载,但实际上,底层的JavaScriptAjax调用可能尚未完成,并且Web Client可能未获得'document.readyState'也等于"complete"

But it seems you have induced set_page_load_timeout(10) in your code which incase the complete page loading (including the JS and Ajax ) doesn't gets completed within 10 seconds will result in a TimeoutException . 但是似乎您在代码中诱发了set_page_load_timeout(10)以防整个页面加载(包括JSAjax )在10秒内未完成将导致TimeoutException That is exactly happening in your case. 这正是您的情况。

Solution

  • If your usecase doesn't have a constraint on Page Load Timeout , remove the line of code set_page_load_timeout(10) 如果您的用页面加载超时没有限制,请删除代码行set_page_load_timeout(10)
  • If your usecase have a dependency on Page Load Timeout , catch the exception and invoke quit() to shutdown gracefully as follows : 如果您的用依赖于页面加载超时 ,请捕获异常并调用quit()以正常关闭,如下所示:

  • Code Block : 代码块:

      from selenium import webdriver driver = webdriver.Chrome(executable_path=r'C:\\path\\to\\chromedriver.exe') driver.set_page_load_timeout(2) try : driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl") print("URL successfully Accessed") driver.quit() except : print("Page load Timeout Occured. Quiting !!!") driver.quit() 
  • Console Output : 控制台输出:

     Page load Timeout Occured. Quiting !!! 

You can find a detailed discussion on set_page_load_timeout() in How to set the timeout of 'driver.get' for python selenium 3.8.0? 您可以在如何设置python硒3.8.0的'driver.get'超时中的set_page_load_timeout set_page_load_timeout()上找到详细的讨论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用chromedriver使用硒在网页中找到这些特定元素? - How do I find these particular elements in a webpage, with selenium using chromedriver? 使用 Selenium Chromedriver 和 python 我如何点击这个元素? - Using Selenium Chromedriver with python how do i click this element? 使用 chrome selenium webdriver 时如何避免超时错误 - How can I avoid a timeout error while using a chrome selenium webdriver 使用selenium和chromedriver时出现BrokenPipeError - BrokenPipeError while using selenium and chromedriver 如何更改选择器以使网站显示登录而不是使用 selenium python 和 chromedriver 进行注册? - How do I change the selector so that the website shows login instead of signup using selenium python and chromedriver? 如何使用 Google Chrome 而不是 Chromedriver Python Selenium - How do I use Google Chrome instead of Chromedriver Python Selenium 如何将 selenium 和 chromedriver 添加到 AWS Lambda function? - How do I add selenium & chromedriver to an AWS Lambda function? 如何解决 TypeError: 'module' object is not callable 错误使用 Selenium 和 ChromeDriver - How to address TypeError: 'module' object is not callable error using Selenium and ChromeDriver 在将Fabric用于以下代码时,如何添加超时? - How do I add timeout while using fabric for below code? Chromedriver 仅支持 BMP 错误中的字符,同时使用 ChromeDriver Chrome 使用 Selenium Python 将 Emoji 发送到 Tkinter 的 label() 文本框 - Chromedriver only supports characters in the BMP error while sending Emoji with ChromeDriver Chrome using Selenium Python to Tkinter's label() textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM