Please help how to switch from one tab to another tab in chrome and to change focus on child tab.
6 Answers
Steps to work with tabs in the same browser:
- Open a new tab using Ctrl + t
- Driver control automatically switches to the newly opened tab
- Perform the required operations here.
- Next switch back to the old tab using Ctrl + Tab. You need to keep pressing this unless you reach the desired tab.
- Once the desired tab is reached, then perform the operations in that tab.
Get the current window handle and open a new tab using Ctrl + t
driver.get("http://google.com");
String windowHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
Check the size of the output of getWindowHandles()
.
Then Use:
ArrayList tabs = new ArrayList (driver.getWindowHandles());
System.out.println(tabs.size());
driver.switchTo().window(tabs.get(0));
The control is now in the new tab-
driver.get("Your application URL");
//perform other operations on new tab.
Switch to the old tab using Ctrl + Tab:
driver.switchTo().window(mainWindowHandle);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
driver.switchTo().defaultContent();
=============================OR============================
You can perform the steps using Action class too.
for navigating left to right side:
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();
For navigating right to left:
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();
To get more details check here.
You can use this code to switch tabs between the parent and child window:
String parent=driver.getWindowHandle();
Set<String>s1=driver.getWindowHandles();
Iterator<String> I1= s1.iterator();
while(I1.hasNext())
{
String child_window=I1.next();
if(!parent.equals(child_window))
{
driver.switchTo().window(child_window);
System.out.println(driver.switchTo().window(child_window).getTitle());
driver.close();
}
}
driver.switchTo().window(parent);
System.out.println(driver.switchTo().window(parent).getTitle());
String parentWindow = driver.getWindowHandle();
For(string childWindow:driver.getWindowHandles())
if(!childWindow.equal(parentWindow))
driver.switchTo().window(childWindow)
Please note: this should work for all the browser's
On my code I click a button and opens a tab (so it is already on the new tab, I don't need to do something to go to that new tab) and run this so it recognize the new tab and worked:
driver.SwitchTo().Window(driver.WindowHandles.Last());
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.close();
driver.switchTo().window(tabs.get2));
-
1Please edit your answer to explain how it will help the OP, and what it adds to the existing answers.– Kate Paulk ♦Jun 5, 2019 at 11:24
You can perform the operation of switching from the parent to child tab using the below code.
Set <String> st= driver.getWindowHandles();
Iterator<String> it = st.iterator();
String parent = it.next();
String child = it.next();
// switch to child
driver.swtichTo().window(child);
Thread.sleep(2000);
//swtich to parent
driver.switchTo().window(parent);
System.out.println("Returned to parent");