3

Please help how to switch from one tab to another tab in chrome and to change focus on child tab.

6

Steps to work with tabs in the same browser:

  1. Open a new tab using Ctrl + t
  2. Driver control automatically switches to the newly opened tab
  3. Perform the required operations here.
  4. Next switch back to the old tab using Ctrl + Tab. You need to keep pressing this unless you reach the desired tab.
  5. 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.

|improve this answer|||||
  • Do we have the switching to tabs functions in Ruby please? – A user Feb 8 '18 at 9:48
0

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());
|improve this answer|||||
0
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

|improve this answer|||||
0

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());
|improve this answer|||||
0
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.close();
driver.switchTo().window(tabs.get2));
|improve this answer|||||
  • 1
    Please edit your answer to explain how it will help the OP, and what it adds to the existing answers. – Kate Paulk Jun 5 '19 at 11:24
0

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");
|improve this answer|||||

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.