Using Selenium I am opening a window. I want to close and Quit the Window after the Button is Clicked. How can I achieve that in Selenium. The function which I want to perform before Closing The window is Below
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.
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:
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. Both are necessary in order to properly terminate a session.
It is recommended that you wrap these methods in a try / finally block to avoid any errors with terminating the session.
if (Driver != null)
{
try { Driver?.CloseApp(); }
finally
{
try
{
Driver?.Quit();
}
finally
{
Driver = null;
DriverFactory.Current.StopAppiumServer();
}
}
}
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.