1

I am trying to switch between two tabs and found following code

 ArrayList<String> tabs2 = new ArrayList<String> (page.getWindowHandles());
     System.out.println(tabs2.size());
        page.switchTo().window(tabs2.get(1));
        page.close();
        page.switchTo().window(tabs2.get(0));

I have one doubt in this. My window is same so page.getWindowHandles() is returning only one handle. tabs2.size is 1 so page.switchTo().window(tabs2.get(1)); is giving exception ArrayIndexOutOfBound

I found similar code in this post switch tabs using Selenium WebDriver with Java but for me this code is giving exception.

Community
  • 1
  • 1
sajid
  • 53
  • 1
  • 9
  • whats the issue here? If you have only 1 tab you are already there on that tab ... and `tabs2.size()` is returning `1` that is correct. – Paras Jun 04 '16 at 13:50
  • I opened two tabs then I am using this code still it's returning 1. then how to work on two tabs? – sajid Jun 04 '16 at 16:50
  • do you open second tab using `selenium` or manually or it is application behavior? – Andersson Jun 04 '16 at 17:44
  • I used this code page.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); – sajid Jun 04 '16 at 18:15

2 Answers2

0

Just try and add a line of code to ensure there are two or more tabs open in the browser :

page.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

Note that the actual keys to send depend on your OS, for example, Mac uses Keys.COMMAND + t, instead of Keys.CONTROL + t.

Post this, the piece of code used by you seems appropriate to switch tabs :

ArrayList<String> tabs2 = new ArrayList<String> (page.getWindowHandles());
System.out.println(tabs2.size());
page.switchTo().window(tabs2.get(1));
page.close();
page.switchTo().window(tabs2.get(0));

Note : I am assuming the page here is the WebDriver instance itself.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Yes thanks dear, I am new to stackoverflow so next time wont make this mistake. I am opening new tab in firefox and my code is specific to firefox only – sajid Jun 05 '16 at 03:55
0
protected void switchTabsUsingPartOfUrl(String platform) {
        String currentHandle = null;
        try {
            final Set<String> handles = driver.getWindowHandles();
            if (handles.size() > 1) {
                currentHandle = driver.getWindowHandle();
            }
            if (currentHandle != null) {
                for (final String handle : handles) {
                    driver.switchTo().window(handle);
                    if (currentUrl().contains(platform) && !currentHandle.equals(handle)) {
                        break;
                    }
                }
            } else {
                for (final String handle : handles) {
                    driver.switchTo().window(handle);
                    if (currentUrl().contains(platform)) {
                        break;
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("Switching tabs failed");
        }
    }

Call this method and pass parameter a substring of url of the tab you want to switch to

Rahul Rana
  • 41
  • 6