Ruban4Axis
Ruban4Axis

Reputation: 841

Closing the Window after Some Time in Selenium Java

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();

Upvotes: 0

Views: 234

Answers (1)

KunLun
KunLun

Reputation: 3225

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());

}

Upvotes: 1

Related Questions