简体   繁体   English

在 Selenium Java 一段时间后关闭 Window

[英]Closing the Window after Some Time in Selenium Java

Using Selenium I am opening a window.使用 Selenium 我正在打开 window。 I want to close and Quit the Window after the Button is Clicked.单击按钮后,我想关闭并退出 Window。 How can I achieve that in Selenium.我怎样才能在 Selenium 中实现这一点。 The function which I want to perform before Closing The window is Below我想在关闭之前执行的 function window 如下

 new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='css-vote-button pds-vote-button' and starts-with(@id, 'pd-vote-button10359300')]/span[text()='Vote']"))).click();

With driver.close() you can close the tab/window which WebDriver controlling.使用driver.close()您可以关闭 WebDriver 控制的选项卡/窗口。

I use this code to close all tabs/windows.我使用此代码关闭所有选项卡/窗口。

var tabs = new ArrayList<>(driver.getWindowHandles());

while(tabs.size() > 0){

    driver.switchTo().window(tabs.get(0)); //NoSuchWindowException

    driver.close();

    tabs = new ArrayList<>(driver.getWindowHandles());

}

To close & quit your driver session, you can implement the following:要关闭并退出您的驱动程序 session,您可以执行以下操作:

    Thread.Sleep(10000) // or Thread.Sleep(TimeSpan.FromSeconds(10));
    Driver.Close();
    Driver.Quit();

Thread.Sleep simply implements 10 second wait, Driver.Close closes your browser session, and Driver.Quit terminates the instance of WebDriver. Thread.Sleep只是实现了 10 秒的等待, Driver.Close关闭浏览器 session, Driver.Quit终止 WebDriver 的实例。 Both are necessary in order to properly terminate a session.为了正确终止 session,两者都是必要的。

It is recommended that you wrap these methods in a try / finally block to avoid any errors with terminating the session.建议您将这些方法包装在 try / finally 块中,以避免在终止 session 时出现任何错误。

            if (Driver != null)
                {
                    try { Driver?.CloseApp(); }
                    finally
                    {
                        try
                        {
                            Driver?.Quit();
                        }
                        finally
                        {
                            Driver = null;
                            DriverFactory.Current.StopAppiumServer();
                        }
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java关闭Selenium Automation中的当前窗口后如何切换到现有窗口 - How to switch to existing window after closing the current window in Selenium Automation using java 基于 winappdriver 的 java selenium 自动化在一段时间后停止工作 - winappdriver based java selenium automation stops working after some time 如何在使用java关闭selenium中的Windows警告框后切换到主窗口 - How to switch to main window after closing a windows alert box in selenium using java 如何建立连接并且在经过一段时间后不关闭它 - How to established a connection and not closing it after passing some time 关闭硒java中的弹出窗口后无法识别对象 - unable to identify object after closing the pop up in selenium java 关闭窗口 (Java) - Closing Window (Java) 在最后一个窗口调用dispose后,java swing程序没有关闭 - java swing program not closing after dispose is called on last window 如何在窗口关闭后正确停止Java applet - How to correctly stop a Java applet after window closing 如何使用Java处理Outlook Mail Window(Desktop App)窗口的关闭并切换回Selenium中的Web应用程序 - How to Handle closing of outlook Mail Window(Desktop App) window and switch back to web application in selenium using java Java ScheduledThreadPool 多线程在一段时间后停止 - Java ScheduledThreadPool multithreads stops after some time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM