0

I am clicking on a link, which results in new tab. I want to move to that tab, close that tab, and then again switch back to Parent window.

I have written following code, its showing error, Actually in my code, control is not moving to child window. Please see following code, and help me:

WebDriver driver;
System.setProperty("Webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");
String parent_window = driver.getWindowHandle();
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click();
Thread.sleep(4000);
String child_window = driver.getWindowHandle();
System.out.println(parent_window);
System.out.println(child_window);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Mamta Gupta
  • 11
  • 2
  • 4

3 Answers3

0

You can also use this kind of code to do, switching between tabs.

public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "Location of chromedriver exe file");

System.out.println("Ready to launch the browser");
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click();
Thread.sleep(4000);

  //store parent window value in string
  String parentWindow = driver.getWindowHandle();

  //store the set of all windows
  Set<String> allwindows= driver.getWindowHandles();

  for (String childWindow : allwindows) {
    if(!childWindow.equals(parentWindow))
      {
        driver.switchTo().window(childWindow);
        System.out.println("child window");
        System.out.println(driver.getTitle());      
        // do some operation
        //Closing the Child Window.
         driver.close();    
     }
    }
    driver.switchTo().window(parentWindow);
    System.out.println("Parent window");
    System.out.println(driver.getTitle());      
}
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
0

Control is not moving to child window because you are not capturing the child window's handle and switching to it. Once the new tab is opened, you need to find all the windows handle using getWindowHandles() method. The first value in the set returned by getWindowHandles() method will be parent window's handle and the second value will be child window's handle. If you want to perform any operation on child window, you need to first switch to it using driver.switchTo().window(). Following code demonstrates, how we can switch to the new tab and then close it:

WebDriver driver;
System.setProperty("Webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");

driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click();
try {
    Thread.sleep(4000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

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

if (handles.size() == 2) {
    Iterator<String> itr = handles.iterator(); 

    String parent_window = itr.next().toString();
    String child_window = itr.next().toString();
    System.out.println(parent_window);
    System.out.println(child_window);

    // switching to child window
    driver.switchTo().window(child_window);

    // closing child window
    driver.close();

    // switching back to parent window
    driver.switchTo().window(parent_window);

} else {
    System.out.println("New tab did not open.");
}

Let me know, if you have any further queries.

Mahipal
  • 900
  • 1
  • 5
  • 7
0

You need to take care of a couple of points as follows:

  • Within System.setProperty() line the Key is webdriver.chrome.driver
  • Within System.setProperty() line the Value must be the absolute path of the WebDriver variant, i.e. of chromedriver.exe
  • To switch() to a new tab, you need to induce WebDriverWait with ExpectedConditions set as numberOfWindowsToBe().
  • Here is your own code with some simple modifications:

    WebDriver driver;
    System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");
    String parent_window = driver.getWindowHandle();
    driver.switchTo().frame("iframeResult");
    driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click();
    new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> windows = driver.getWindowHandles();
    for(String child_window:windows)
        if(!parent_window.equals(child_window))
        {
            driver.switchTo().window(child_window);
            System.out.println(child_window);
        }
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352