标题说明了一切。我想在WebDriver 4.8.1中侦听浏览器上的窗口关闭事件。我尝试查看WebDriverListener
,但没有任何运气。以下是我的MCVE:
public class Test implements WebDriverListener {
private Object lock = new Object();
public Test() throws InterruptedException, MalformedURLException {
WebDriver chrome = new ChromeDriver();
EventFiringDecorator<WebDriver> decorator = new EventFiringDecorator<WebDriver>(this);
WebDriver driver = decorator.decorate(chrome);
driver.navigate().to(new URL("https://www.google.com/"));
driver.manage().window().maximize();
synchronized (lock) {
lock.wait();
}
}
public void beforeAnyWindowCall(Window window, Method method, Object[] args) {
// Prints: maximize null
System.out.println(method.getName() + " " + Arrays.toString(args));
}
public void beforeClose(WebDriver driver) {
// Never is called
System.out.println("Closing");
synchronized (lock) {
lock.notifyAll();
}
}
public static void main(String[] args) throws InterruptedException, MalformedURLException {
new Test();
// Never is called
System.out.println("Closed");
}
}
在Swing中,我们有WindowListener#windowClosed(WindowEvent)
,它在窗口关闭时被触发。在Selenium WebDriver中可以完成类似的事情吗?澄清一下,我不是想知道WebDriver
何时关闭,而是想知道 window 何时关闭。(手动)关闭。另外,我不是在寻找一个方法,我调用来确定 * 如果 * 窗口关闭。我想一个事件,以激发 * 当 * 关闭。
我看了下面的问题,但找不到一个有效的答案:
2条答案
按热度按时间4urapxun1#
你可以使用下面的方法,当浏览器意外终止时,抛出UnreachableBrowserException异常
6za6bjd02#
使用try-catch,如下所示:
控制台输出: