Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:27:22+00:00 2026-06-07T16:27:22+00:00

I use Selenium WebDriver. I open the first page then open the second page

  • 0

I use Selenium WebDriver. I open the first page then open the second page – perform some action and go back to first page. Before I want to close the second page I use the command driver.close();, but it closes the first page instead of the second. 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("001")).click();
 driver.close();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:27:24+00:00Added an answer on June 7, 2026 at 4:27 pm
        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 works for me…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Selenium WebDriver with Java. I want to use Safari browser. Does Selenium
Hello all I am using webdriver so if I want to use selenium;s rc
I am trying to use Selenium IDE to record action and then make them
I am using Selenium RC, TestNG, Java with Eclipse. I want to use WebDriver.
I use selenium webdriver. I have such code - driver.findElement(By.name(ctsk)).sendKeys(1234); . It plus some
I'm trying to use Selenium WebDriver to input text to a GWT input element
When I use Selenium 2 code (Java) to open Firefox (or any other browser)
For integration testing I want to use selenium, also for my TDD/BDD workflow but
I use Selenium to test some web-product in my company. Product has button, which
I'm attempting to use the selenium-webdriver [ruby bindings][1] to access an internal web-site that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.