1

When clicked on a button, a new window opened but I am unable to perform any operation on this window.

How do I get selenium to use the new window?

code:

       System.setProperty("webdriver.gecko.driver","D:\\selenium\\geckodriver.exe");
        FirefoxDriver driver = new FirefoxDriver();
        driver.get("base url");
        driver.navigate().to("new tab");
        driver.findElement(By.xpath(xpathExpression)).click();//While clicking on the button new window is opening 
        //code to perform operation on new window
        driver.findElement(By.xpath(xpathExpression)).click();
  • Please add your code – Rutvi Soni Jul 10 '17 at 6:28
0

Solution:

  • To switch control from child & parent window, we can use windowHandlers.
  • If there are multiple child windows you want to access, then iterators will be helpful to navigate
  • Hope, below snippet will help you:

    Set <String> handles =wd.getWindowHandles();
    Iterator<String> it = handles.iterator();
    //iterate through your windows
    
    String parent = it.next();
    System.out.println("parent window:"+parent);
    
    String newwin = it.next();
    System.out.println("new window:"+newwin);
    
    wd.switchTo().window(newwin);
    
    System.out.println("Confirm you are on new window"+wd.getTitle());
    
    Thread.sleep(5000);
    
    //perform actions on new window here
    //perform actions on new window here
    //perform actions on new window here
    
    wd.close();
    wd.switchTo().window(parent);
    
0

The idea, You are not able to deal with controls in new opened window, Because it may be a Frame control, and you should switch to that frame window first to handle the objects inside it.

Please check the answer here in this similar post

0

Try this below code. This should work

Set<String> winHandles = driver.getWindowHandles();
        for (String wHandle : winHandles) {
            driver.switchTo().window(wHandle);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.