[英]Webdriver - How to check if browser still exists or still open?
我想檢查瀏覽器是否仍然存在,如果不存在,那么我想打開一個新的瀏覽器! webdriver中是否有可用的api來檢查瀏覽器是否仍然存在?
調用driver.close()
后,driver 的值被設置為
FirefoxDriver: firefox on WINDOWS(4b4ffb1e-7c02-4d9c-b37b-310c771492ac)
但是如果你調用driver.quit()
那么它會將 driver 的值設置為
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
}
沒有它的api。 最好的方法是調用toString
方法,該方法返回這樣的字符串:
SafariDriver . . . null
然后你可以調用contains
方法,它會檢查字符串null
是否在那里。
請注意,這僅在調用quit
時才有效。
我積極將其用於 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')
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.