我想检查浏览器是否仍然存在,如果它不是,那么我想打开一个新的浏览器! webdriver中是否有api可用于检查浏览器是否仍然存在?
答案 0 :(得分:5)
调用driver.close()
后,驱动程序的值设置为
FirefoxDriver: firefox on WINDOWS(4b4ffb1e-7c02-4d9c-b37b-310c771492ac)
但是如果你打电话给driver.quit()
,那么它会将驱动程序的值设置为
FirefoxDriver: firefox on WINDOWS (null)
因此,如果您在调用 driver.quit()之后检查浏览器窗口,那么您将能够通过以下实现了解。
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.quit();
if(driver.toString().contains("null"))
{
System.out.print("All Browser windows are closed ");
}
else
{
//open a new Browser
}
答案 1 :(得分:3)
它没有api。最好的一个,你可以做的是调用toString
方法,它返回一个这样的字符串:
SafariDriver . . . null
然后你可以调用contains
方法,它会检查字符串null
是否存在。
请注意,只有在调用quit
时才会有效。
答案 2 :(得分:1)
我积极地将其用于Chrome。同时,由于我使用cmd title运行浏览器,因此可以关闭命令行以消除过多的负担。
from selenium.common.exceptions import WebDriverException
while True:
try:
#do somethings
except selenium.common.exceptions.WebDriverException as e:
if 'chrome not reachable' in str(e):
os.system('taskkill /FI "WindowTitle eq YourTitleIfExistsOrDeleteThisLine*" /T /F')