4

This is the code for transferring control

for (String handle : driver.getWindowHandles()) { 
    driver.switchTo().window(handle);

    ArrayList<String> windowTab = new ArrayList<String>(driver.getWindowHandles());

    driver.switchTo().window(windowTab.get(1));

    String url = driver.getCurrentUrl();

    System.out.println(url);
}

But the issue is that while am printing the URL, it prints two times. After giving the control to the new window I have to perform all operations in that window.

Is this enough for doing that or are there any modifications I have to do?

  • Any specific reason you are using getWindowHandles two times? – demouser123 Sep 15 '15 at 6:32
  • No. I just want to transfer the control to the newly opened window. – kripindas Sep 15 '15 at 7:02
  • so why using it two times, wouldn't it transfer the control in the first instance itself? You are transferring control two times. That is why I asked. – demouser123 Sep 15 '15 at 8:05
  • If am using a single handle means its first taking the Control of First Instance but if am giving the second means, in the first attempt itself it will take the control to the newly opened window. – kripindas Sep 15 '15 at 8:20
1

This works for me:

    Set <String> handles =driver.getWindowHandles();
    Iterator<String> it = handles.iterator();

    String parent = it.next();
    String child = it.next();

    driver.switchTo().window(child);

    //perform actions on child window
    //perform actions on child window

    driver.close(); // only for child wondow


    driver.switchTo().window(parent);

    //perform actions on parent window
    //perform actions on parent window

    driver.quit(); // After execution of main thread and for parent window
0
protected BasePageObject<?> loadSimulteneousBrowser(String url, boolean session) throws IOException
{
    if (session)
    {
        selenium_simultaneous = getSelenium(ChromeProfile.WITH_SESSION, false);
    }
    else
    {
        selenium_simultaneous = getSelenium(ChromeProfile.WITHOUT_SESSION, false);
    }
    if ("".equalsIgnoreCase(url))
    {
        selenium_simultaneous.get(APPLICATION_URL);
    }
    else
    {
        logger.info("URL ---> " + url);
        selenium_simultaneous.get(url);
    }
    try
    {
        resultpage_Simulteneous = new BasePageObject<>(selenium_simultaneous);// BasePage is for Detected Current Page
        timeout(1);
        resultpage_Simulteneous = resultpage_Simulteneous.detectPage(); // Here BasePage object get 1 page and pass it into result.
        timeout(1);
        logger.info("result Page:" + resultpage_Simulteneous);
        return resultpage_Simulteneous;
    }
    catch (Exception e)
    {
        logger.error(e);
    }
    return resultpage_Simulteneous;
}
  • Please edit your answer to explain how your code solves the OP's problem. – Kate Paulk Nov 17 '17 at 12:32
0

Looking at your code, I think this line is not needed:

ArrayList<String> windowTab = new ArrayList<String>(driver.getWindowHandles());

If you want to switch tabs use code like the example below:

String currentscreen = driver.getWindowHandle(); //for identifying the current window
Set<String> tabSet = driver.getWindowHandles(); //to get total window
for (String tab : tabSet) {
    if (!tab.equals(currentscreen)) {
       driver.switchTo().window(tab);
       //and do the actions you need here  
       String url = driver.getCurrentUrl();
       System.out.println(url);
     }
}

It prints the URL only one time if you have only one different tab compared to the current tab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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