如何为python selenium 3.8.0设置'driver.get'的超时?

Ale*_*lex 4 python selenium webdriver selenium-webdriver pageloadtimeout

在selenium测试中,您使用打开网页

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("whateverpage.org.com")
Run Code Online (Sandbox Code Playgroud)

如何为selenium版本3.8.0和python 2.7.12 设置此命令的超时?

Deb*_*anB 10

设置time outPage Loading你可以诱导set_page_load_timeout(seconds).


set_page_load_timeout


方法细节

def set_page_load_timeout(self, time_to_wait):
    """
    Set the amount of time to wait for a page load to complete
    before throwing an error.
Run Code Online (Sandbox Code Playgroud)

ARGS

time_to_wait: The amount of time to wait
Run Code Online (Sandbox Code Playgroud)

用法

driver.set_page_load_timeout(3)
Run Code Online (Sandbox Code Playgroud)

from selenium import webdriver
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\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 TimeoutException as e:
    print("Page load Timeout Occured. Quiting !!!")
    driver.quit()
Run Code Online (Sandbox Code Playgroud)

控制台输出

Page load Timeout Occured. Quiting !!!
Run Code Online (Sandbox Code Playgroud)

文档

你可以在pageLoadTimeout这里找到详细的讨论pageLoadTimeout in Selenium not working


深潜

根据Python,3.x如果我们不处理异常,则会观察到以下日志消息:

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
  (Session info: chrome=62.0.3202.94)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.2.9200 x86_64)
Run Code Online (Sandbox Code Playgroud)