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
4,813 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 29, 2019 in Selenium by Anvi
• 14,150 points
3,242 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 6, 2020 in Selenium by Sri
• 3,190 points
16,042 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
26,009 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,500 points
13,686 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,190 points
9,149 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,910 points
7,228 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 3, 2020 in Selenium by Sri
• 3,190 points
7,880 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
2,424 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 13,716 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP