我正在使用selenium + python,在python上使用隐式等待和try / except代码来捕获错误。但是我注意到如果浏览器崩溃(假设用户在程序执行期间关闭了浏览器),我的python程序将挂起,并且隐藏等待的超时似乎不起作用发生。以下过程将永远留在那里。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
import datetime
import time
import sys
import os
def open_browser():
print "Opening web page..."
driver = webdriver.Chrome()
driver.implicitly_wait(1)
#driver.set_page_load_timeout(30)
return driver
driver = open_browser() # Opens web browser
# LET'S SAY I CLOSE THE BROWSER RIGHT HERE!
# IF I CLOSE THE PROCESS HERE, THE PROGRAM WILL HANG FOREVER
time.sleep(5)
while True:
try:
driver.get('http://www.google.com')
break
except:
driver.quit()
driver = open_browser()
答案 0 :(得分:0)
如果获取Google主页时出现异常,您提供的代码将始终挂起。 可能发生的事情是,尝试获取谷歌主页会导致异常,通常会暂停程序,但是您正在使用except子句掩盖它。
尝试对您的循环进行以下修正。
max_attemtps = 10
attempts = 0
while attempts <= max_attempts:
try:
print "Retrieving google"
driver.get('http://www.google.com')
break
except:
print "Retrieving google failed"
attempts += 1