gpt4 book ai didi

python - 如果 web 浏览器在 selenium 中崩溃,进程会挂起

转载 作者:行者123 更新时间:2023-12-03 17:39:33 25 4
gpt4 key购买 nike

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

关于python - 如果 web 浏览器在 selenium 中崩溃,进程会挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39627390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com