问题描述
我在这里可能缺少一些简单的东西,但是我已经尝试了很多,没有任何运气.我是硒新手,无法解决以下问题.使用get()导航到网页时,我不断收到超时消息.页面可以正确加载,但是页面上的所有内容加载完毕后(我认为这可能与广告加载所需的加载时间有关),我收到了此错误消息.
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. 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:消息:超时 (会议信息:chrome = 65.0.3325.181) (驱动程序信息:chromedriver = 2.36.540470(e522d04694c7ebea4ba8821272dbef4f9b818c91),platform = Windows NT 10.0.16299 x86_64)
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)
我尝试了以下方法;移动chromedriver位置,尝试使用较旧版本的硒,进行等待,隐式等待,time.sleep等.任何输入都会很棒,因为这似乎很简单,我希望尽快解决.
I have tried the following; moving chromedriver location, trying older versions of selenium, waits, implicitly waits, time.sleep and others. Any input would be great as this seems like something simple and I would like to get it fixed soon.
有问题的代码:
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()
推荐答案
根据您的问题,使用get()导航至网页时,看来页面已获得加载正确,但实际上 JavaScript 和 Ajax调用可能尚未完成,并且 Web客户端可能未实现 'document.readyState'
也等于"complete"
.
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.
但是似乎您在代码中引入了 set_page_load_timeout(10),以防加载整个页面(包括 JS 和 Ajax ) 10秒内未完成将导致 TimeoutException .这正是您的情况.
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. That is exactly happening in your case.
- 如果您的用例对页面加载超时没有限制,请删除代码行
set_page_load_timeout(10)
-
如果您的用例依赖于页面加载超时,请捕获异常并按如下所示调用
quit()
正常关闭:
- If your usecase doesn't have a constraint on Page Load Timeout, remove the line of code
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 :
代码块:
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 !!!
您可以在
这篇关于将chromedriver与Selenium一起使用时,如何更正超时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!