0

I am having problem understanding the syntax of how to switch between windows. The standard syntax is:

Set <String> stringHandles = driver.getWindowHandles();
for (String handle:stringHandles)
{driver.switchTo().window(handle)}

However, I think that it will only work, if the first element in the Set is a the primary window. If so,in for loop it will first switch to itself and only then to the secondary window. Am I right so far? Also, I see that if the first element in the Set is the secondary window, then it will switch to secondary and then it will go back to the primary window.Thus, the syntax above is imperfect and to make sure that we switch to the secondary window, we need to store the value of the primary window and then insert a condition that the handle of the window than we want is not equal to the value of the original window. Am I right in my reasoning?

1
  • Is it question or discussion ? I am not seeing any clear question here.
    – Sagar007
    Commented Nov 28, 2017 at 5:45

1 Answer 1

1

Yes You are right. You need to store the value of the primary window and then insert a condition that the handle of the window is not equal to the value of the original window.

Code Snippet

    // Storing parent window reference into a String Variable  
    String Parent_Window = driver.getWindowHandle();

    //Collecting all the windows into a Set variable (as each window is unique)    
    Set <String> stringHandles = driver.getWindowHandles();

    for (String handle:stringHandles)
    {
       // Verifying current window is not equal to original window and switching to the window
       if (handle != Parent_Window)
        {
            driver.switchTo().window(handle)
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.