-1

I am trying to automate a scenario where when I click on a link another tab opens with details.

Question 1 : Do I have to specifically set my focus to the 2nd tab or selenium automatically finds the element in the 2nd tab?

I am using the below code to set the focus to the 2nd tab :

String currentWindow = driver.getWindowHandle();
driver.switchTo().window(currentWindow);

Problem : I am getting an error that selenium is unable to find the specified element.

Could you guys suggest me what am I doing wrong, and the best way to switch to 2nd tab.

Guy
  • 46,488
  • 10
  • 44
  • 88
  • Possible duplicate of [switch tabs using Selenium WebDriver with Java](http://stackoverflow.com/questions/12729265/switch-tabs-using-selenium-webdriver-with-java) – Jainish Kapadia Feb 28 '17 at 08:42

2 Answers2

1

Actually, you are setting the focus on the first tab, not the second one. You need to do something like this

String currentWindow = driver.getWindowHandle();

// open the new tab here

for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(currentWindow)) {
        driver.switchTo().window(handle);
    }
}

And the answer to your question is yes, you have to tell the driver to set its focus on the new tab.

Guy
  • 46,488
  • 10
  • 44
  • 88
0

You can get all the window handles as handlers=driver. GetWindowHandles() which will return all the handler string. Then using index switch to the appropriate handle using driver.switchto().window(handlers[1])