Selenium webdriver与python-如果加载太长时间如何重新加载页面?

时间:2014-08-07 02:22:11

标签: python firefox selenium timeout webdriver

driver = webdriver.Firefox()               #opens firefox
driver.get("https://www.google.com/")      #loads google

如果加载谷歌需要很长时间,如何关闭浏览器并从头开始编写代码?

1 个答案:

答案 0 :(得分:8)

通过set_page_load_timeout()设置页面加载超时并捕获TimeoutException

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

driver = webdriver.Firefox()
driver.set_page_load_timeout(10)
while True:
    try:
        driver.get("https://www.google.com/")
    except TimeoutException:
        print "Timeout, retrying..."
        continue
    else:
        break

另请参阅:How to set Selenium Python WebDriver default timeout?