How do I close a focused window using Selenium WebDriver in Java

0 votes

I work with Selenium WebDriver. I initially open the first page then move on to the second page and perform some action and go back to the first page. In this case, I want to close the first page and work on the second. I use the command driver.close() to do this, but, it closes the second page instead of the first. How can I make Selenium to close a specific window?

Part of code

String HandleBefore = driver.getWindowHandle();

 driver.findElement(By.xpath("...")).click();
 for (String twohandle : driver.getWindowHandles()) {
        driver.switchTo().window(twohandle);
    }       
 driver.findElement(By.linkText("abc")).click();
 driver.close();
May 9, 2019 in Selenium by Surya
• 970 points
2,649 views

1 answer to this question.

0 votes

You can use this   

String base = driver.getWindowHandle();

Set <String> set = driver.getWindowHandles();

    set.remove(base);
    assert set.size()==1;
driver.switchTo().window(set.toArray(new String[0]));
driver.close();
driver.switchTo().window(base);

This is easy and it works for me

Hope this helps

answered May 10, 2019 by Vaishnavi
• 1,180 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

How can I refresh a browser window in different ways using Selenium Webdriver?

Hello Piyush, you can refresh a browser ...READ MORE

answered May 28, 2019 in Selenium by Anvi
• 14,130 points
1,507 views
0 votes
2 answers

How to open a browser window in full screen using Selenium WebDriver with C#

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 5, 2020 in Selenium by Sri
• 3,010 points
11,228 views
0 votes
1 answer

How do I get current URL in Selenium Webdriver using Python?

Use current_url element. Example: print browser.current_url READ MORE

answered Aug 8, 2018 in Selenium by Meci Matt
• 9,460 points
21,303 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
4,614 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,465 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
3,294 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
3,896 views
0 votes
1 answer

How do I increase or decrease the sleep time in clickAndWait command in selenium IDE?

If you are using Selenium Webdriver, you have ...READ MORE

answered Feb 28, 2019 in Selenium by Vaishnavi
• 1,180 points
891 views
0 votes
1 answer

How to auto-refresh the ChromeDriver using Selenium Webdriver?

You can use this command. Also, refresh ...READ MORE

answered May 10, 2019 in Selenium by Vaishnavi
• 1,180 points

edited May 10, 2019 by Omkar 7,185 views