简体   繁体   中英

Checking whether Selenium WebDriver points to a window or not

Consider the below code:

getCurrentUrl(driver);
driver.close();
getCurrentUrl(driver);

public void getCurrentUrl (WebDriver driver) {
    if (driver window is not closed)
        System.out.println(driver.getCurrentUrl());
    else
        System.out.println("Window is not available");
}

Please let me know how to perform the "driver window is not closed" check in Java.

hmmm ... Consider this just as a guess. But I feel like that when you call driver variable after you close it, you can get NullPointerException because it will be null

But anyway. I would implement it like this:

public void getCurrentUrl (WebDriver driver) {
try {
   System.out.println(driver.getCurrentUrl());
} catch (Exception e) { // the most top one
   System.out.println("Window is not available");
  }
}

There is possibly better and cleaner way of doing it. But I am not Java programmer and my gut feeling tells me this will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM