0
votes

I am trying to switch to parent window from pop-up window which is generated by a click.

I have tried some methods which I found but non of them worked.

public static String validateHierarchy(Properties prop)
{
    String Hierarchy = driver.findElement(By.xpath("html/body/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[2]/td[2]/table/tbody/tr/td[1]/table/tbody/tr[34]/td")).getText();
    Log.info(Hierarchy);

    String mainWindowHandle = driver.getWindowHandle();
    driver.findElements(By.cssSelector("a[class='left-link']")).get(10).click();
    //switching to current window 
    Set<String> s = driver.getWindowHandles();
    System.out.println("Window"+mainWindowHandle);
    System.out.println("Window"+s);
    Iterator<String> ite = s.iterator();
    while(ite.hasNext())
    {
        String popupHandle=ite.next().toString();
        if(!popupHandle.contains(mainWindowHandle))
        {
            driver.switchTo().window(popupHandle);
            //checking for forgot password text in the current page
            String part1 = driver.findElement(By.xpath("html/body/div[3]/form")).getText();
            Log.info(part1);
            String part2 = driver.findElement(By.xpath(".//*[@id='tree']")).getText();
            Log.info(part2);

            driver.close(); //closing child window
        }
    }

     driver.switchTo().window( mainWindowHandle );

    String mainWindowHandle1 = driver.getWindowHandle();
    System.out.println("Window"+mainWindowHandle1);
    driver.findElements(By.cssSelector("a[class='left-link']")).get(11).click();




    //switching to current window 
    Set <String> s1 = driver.getWindowHandles();
    System.out.println("Window"+mainWindowHandle1);
    System.out.println("Window"+s1);
    Iterator<String> ite1 = s1.iterator();
    while(ite1.hasNext())
    {
        String popupHandle1=ite1.next().toString();
        if(!popupHandle1.contains(mainWindowHandle1))
        {
            driver.switchTo().window(popupHandle1);

            //checking for forgot password text in the current page
            String part1 = driver.findElement(By.xpath("html/body/div[3]/form")).getText();
            Log.info(part1);
            String part2 = driver.findElement(By.xpath(".//*[@id='tree']")).getText();
            Log.info(part2);



            driver.close(); //closing child window

         }
      }


    driver.switchTo().window( mainWindowHandle );
    return Hierarchy;
}

The first pop-up is handled properly but when it comes out of that and goes to the click option "get(11).click()". It gets error there saying

Window{ed1019d1-0ac5-4cc0-ba18-8efdadc10c4c} Window[{ed1019d1-0ac5-4cc0-ba18-8efdadc10c4c}, {9e9cbcf5-9c4b-4805-918b-8f3eb7d9f946}] Window{ed1019d1-0ac5-4cc0-ba18-8efdadc10c4c} Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at JaamoonManage.validateHierarchy(JaamoonManage.java:299) at JaamoonManage.main(JaamoonManage.java:106)

Even i also tried to find the String Hierarchy element but i was not able to find that also. It is switching to the parent window but it is not able to do any on that.

Can someone point out where i am having problem.

Thanks.

1
I have tried that 1 st method but it didnt help.. - avisek
I don't understand how i am getting IndexOutOfBoundException - avisek

1 Answers

0
votes

Reason for getting IndexOutOfBoundException is, there was no 11th element with this locator By.cssSelector("a[class='left-link']")

get method in List will returns the element at the specified position in this list and throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

Check your logic at below step once again.

 driver.findElements(By.cssSelector("a[class='left-link']")).get(11).click();

Refer this blog to for switching control to child window.