[英]Webdriver - How to check if browser still exists or still open?
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来检查浏览器是否仍然存在?
After calling driver.close()
the value of driver is set to调用
driver.close()
后,driver 的值被设置为
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
}
There is no api for it.没有它的api。 The best one, you can do is call
toString
method, which returns a string like this:最好的方法是调用
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
时才有效。
I actively use this for Chrome.我积极将其用于 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.
同时,由于我用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')
public void isBrowserWindowOpen(WebDriver dr){
RemoteWebDriver driver = (RemoteWebDriver) dr;
try {
driver.getWindowHandles();
} catch (NullPointerException | NoSuchSessionException e) {
//open a new Browser
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.