[英]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()导航至网页时遇到的问题,显然页面似乎已正确加载,但实际上,底层的JavaScript和Ajax调用可能尚未完成,并且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) , 以防整个页面加载(包括JS和Ajax )在10秒内未完成将导致TimeoutException 。 That is exactly happening in your case.
这正是您的情况。
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.