Selenium Web Driver : How to transfer control to the new tab that opens on top of existing/current tab

Hello Masters,
I am new to selenium automation and this is my first question to the forum. I am currently automating a web application, basically starting with the the home page that has several links at top/ribbon to navigate to different pages. Where in one of the link navigates to web outlook, on click of this link it reloads as a tab on top of current tab(home page) within same browser window.

Currently my automation script failing when it clicks on the link that opens the web outlook page. The point to Note is “the browser tab is opening on top of current tab”, the automation run fails as its not able to locate element on the reloaded tab

I am not exactly sure, but I feel the control is not getting transferred to the reloaded tab.
I am also confused why would it need to transfer control as it still in the same browser window

some more info:
I am testing in IE browser, selenium web driver, using java, I have not tried windows handle/ switch as I don’t know how to use them.

please suggest with code…
Appreciate quick help/ solution on this problem.

Add Comment
2 Answer(s)

List<String> wHandle = new ArrayList<String> (driver.getWindowHandles());
Set<String> wHandleSize = driver.getWindowHandles();
if (wHandleSize.size()>1) {
driver.switchTo().window(wHandle.get(1));
Log.info(“Switching to newly opened window”);
}
else
Log.info(“No new tab is opened”);

amitjain Supporter Answered on November 23, 2015.
Add Comment

To handle multiple handles, you can first store your main window’s windowhandle using i.e.

String MainWin = driver.getWindowHandle();

and to identify MainWin, you can use above solution and one by one compare Windows handles by comparing it with MainWin variable.

List<String> wHandle = new ArrayList<String> (driver.getWindowHandles());
Set<String> wHandleSize = driver.getWindowHandles();

for (String handle : wHandles)
{
       if(handle.contains(MainWin)
       {
             driver.switchTo().window(handle);
             Log.info(“Main Window Selected”);
        }
}

nimesh2207 Train Answered on December 9, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.