Steps to work with tabs in the same browser:

  1. Open a new using Ctrl + t
  2. Driver control automatically switches to the newly opened tab
  3. Perform the required operations here.
  4. Next 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.



Source link https://sqa.stackexchange.com/questions/26800/how-to-switch-from-one-tab-to-another-tab-in-

LEAVE A REPLY

Please enter your comment!
Please enter your name here