driver = webdriver.Firefox() #opens firefox
driver.get("https://www.google.com/") #loads google
如果它需要很长时间来加载谷歌,我如何使它关闭浏览器,并从头开始的代码?
通过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
参见:如何设置Selenium Python WebDriver默认超时时间?