Yoda
Yoda

Reputation: 18068

Focus on newly opened tab

I click a login button in my app that causes new tab to be openned but altough Chrome displays content of the new tab ChromeDriver still is operates on the old tab.

Question: How to make ChromeDriver "follow" newly opened tab so I can interact with elements in it?

Upvotes: 0

Views: 1373

Answers (4)

frianH
frianH

Reputation: 7563

You need switch first, use .switchTo().window:

//handle original window
String original = driver.getWindowHandle();

//do something here to bring new window
btn.click();

for(String handle: driver.getWindowHandles()) {
    driver.switchTo().window(handle);
    if(!driver.getWindowHandle().equals(original)) {
        //perform with new window

    }
}

//back to original window
driver.switchTo().window(original);

With the above code you can switch again to original window.

Upvotes: 0

H Pat
H Pat

Reputation: 134

    String winHandleNew = driver.getWindowHandle();
    //Go through loop to get handle of newest window opened
    for(String winHandle : driver.getWindowHandles()){
        winHandleNew = winHandle;
    }
    // Switch to newest window opened
    driver.switchTo().window(winHandleNew);

Upvotes: 1

Krishnendu Das
Krishnendu Das

Reputation: 11

You just need to switch to the new tab.

By default selenium driver will stay on the first tab that the driver has opened.

Selenium uses these 2 methods to handle it.

  1. driver.getWindowHandle() [It will get the current tab handle]
  2. driver.getWindowHandles() [It will get all the tab handles that are open]

So, you need to store all the tabs in a variable and handle them one by one.

See the below example.

//I am using a set string "allWindowHandles1" to store all the tabs.
//I am using a simple string "handle1 " to handle the tabs one by one[if present]
//You can use the below for each loop in future if there is multiple child windows also
Set<String> allWindowHandles1 = driver.getWindowHandles();
for(String handle1 : allWindowHandles1) {
    Thread.sleep(2000);
    driver.switchTo().window(handle1);

    //You can write your code to handle the elements in the child window here
    //Now the driver will be in your child window
    Thread.sleep(2000);
}

Upvotes: 1

keinz
keinz

Reputation: 103

when you open another tab using selenium, even though it opened another tab it will still operate on the parent tab(the one that is inside/invoked in the get method) in order to operate on the child tab(newly opened tab) you need to do this

    //storing all the windows opened by default parent is index 0
    Set<String>ids=driver.getWindowHandles(); 
    Iterator<String> it = ids.iterator();
    String parentId = it.next(); //1st one = parent tab
    String childId = it.next(); //2nd one = child tab

    driver.switchTo().window(childId); //switch to child window 
    System.out.println(driver.getTitle());
    driver.switchTo().window(parentId); //switches back to parent window

Upvotes: 1

Related Questions