java Webdriver - 如何检查浏览器是否仍然存在或仍然打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27616470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webdriver - How to check if browser still exists or still open?
提问by user2048204
I want to check if browser still exists and if it isn't then i want to open a new browser! Is there a api available in webdriver to check if the browser still exists?
我想检查浏览器是否仍然存在,如果不存在,那么我想打开一个新的浏览器!webdriver中是否有可用的api来检查浏览器是否仍然存在?
采纳答案by Rupesh Shinde
After calling driver.close()
the value of driver is set to
调用driver.close()
后驱动的值设置为
FirefoxDriver: firefox on WINDOWS(4b4ffb1e-7c02-4d9c-b37b-310c771492ac)
But if you call driver.quit()
then it sets the value of driver to
但是如果你打电话,driver.quit()
那么它会将 driver 的值设置为
FirefoxDriver: firefox on WINDOWS (null)
So if you're checking the browser window after calling driver.quit()then you will be able to know by below implementation.
因此,如果您在调用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
}
回答by Ant's
There is no api for it. The best one, you can do is call toString
method, which returns a string like this:
没有它的api。最好的toString
方法是调用方法,它返回一个这样的字符串:
SafariDriver . . . null
Then you can call contains
method, which does check in the string null
is there.
然后你可以调用contains
方法,它会检查字符串null
是否存在。
Note that this will work only if the quit
is been called.
请注意,这仅在quit
被调用时才有效。
回答by Faz?l Akbulut
I actively use this for Chrome. At the same time, since I run the browsers with cmd title, I can close the command line to get rid of excessive loads.
我积极将其用于 Chrome。同时,由于我用cmd标题运行浏览器,我可以关闭命令行以摆脱过度负载。
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')