driver = webdriver.Firefox() #opens firefox
driver.get("https://www.google.com/") #loads google
如果加载谷歌需要很长时间,如何关闭浏览器并从头开始编写代码?
答案 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