问题
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: Message: timeout (Session info: chrome=65.0.3325.181) (Driver info: 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. 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()
回答1:
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.
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.
Solution
- 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 :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?
来源:https://stackoverflow.com/questions/49640060/while-using-the-chromedriver-with-selenium-how-do-i-correct-a-timeout-error