如何设置python硒3.8.0的'driver.get'超时? [英] How to set the timeout of 'driver.get' for python selenium 3.8.0?

查看:168
本文介绍了如何设置python硒3.8.0的'driver.get'超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在硒测试中,您使用

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("whateverpage.org.com")

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

How can I set the timeout of this command for selenium version 3.8.0 and python 2.7.12?

推荐答案

要为 Page Loading 设置 time out ,您可以诱导 .

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.


Args

time_to_wait: The amount of time to wait


用法

driver.set_page_load_timeout(3)


示例

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()


控制台输出

Page load Timeout Occured. Quiting !!!


文档

您可以在 pageLoadTimeout 上找到详细讨论,


Documentation

You can find a detailed discussion on pageLoadTimeout here pageLoadTimeout in Selenium not working

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

As per Python 3.x if we don't handle the exception the following log messages are observed :

    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)

这篇关于如何设置python硒3.8.0的'driver.get'超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆