Python 如果selenium中的web浏览器崩溃,进程将挂起

Python 如果selenium中的web浏览器崩溃,进程将挂起,python,selenium,Python,Selenium,我正在使用selenium+python,在python上使用隐式等待和try/except代码来捕获错误。然而,我注意到,如果浏览器崩溃(比如说,用户在程序执行期间关闭浏览器),我的python程序将挂起,并且当这种情况发生时,隐式等待的超时似乎不起作用。下面的过程将永远停留在那里 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import

我正在使用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()

如果出现获取google主页的异常,您提供的代码将始终挂起。 可能发生的情况是,试图获取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
max\u attemtps=10
尝试次数=0

当套接字连接关闭时,这似乎是一个错误,析构函数应该会杀死驱动程序。现在是2019年,它仍然会发生。你找到用Python处理它的方法了吗?在R-Selenium中,我可以成功捕获浏览器崩溃并返回Selenium所在的页面。代码挂起在“try”部分,但从未到达“except”。这意味着不会引发异常,但当浏览器因某种原因关闭,并且selenium查找元素或尝试打开页面时,不会引发异常,代码挂起。我已经编辑了代码,我正在做的除了部分,它只是从来没有达到它。