samedi 7 avril 2018

Unable to fetch Anchor Tags in WebDriver

I have this piece of code which is supposed to fetch the all the anchor tag and click on it. Since the link gets open up in new tab there is a getWindowHandle code. Upon running the code i get "ElementNotVisibleException" error. I want to know where am i going wrong.

public void FetchingAllHomePageLinks() throws Exception
  {
        /* Anchor Tag(a) is a WebElement, hence its stored in the form of list  */
        List<WebElement> linkElements = driver.findElements(By.tagName("a"));
        /* Array of string stores the linkelements size */
        String[] linkTexts = new String[linkElements.size()];

        int i=0;
        //For every webelements consist of linkelements, its text has been stored in linktext
        for(WebElement e : linkElements)
        {
            linkTexts[i]=e.getText();
            i++;
        }

        for(String T : linkTexts)
        {
            if(!T.equals(null) || !T.equals("") || !T.isEmpty())
            {
                wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText(T))).click();
                //Since the link if clicked opens in new tab hence getWindowHandles Code
                String parentWindow = driver.getWindowHandle();   
                  System.out.println("Parent Window ID is : " + parentWindow);

                  Set<String> allWindow = driver.getWindowHandles();

                  int count = allWindow.size();
                  System.out.println("Total Window : " + count);

                  for(String child:allWindow)
                  {
                      if(!parentWindow.equalsIgnoreCase(child))
                      {
                          driver.switchTo().window(child);
                          if(driver.getTitle().contains(errorTitle))
                            {
                                System.out.println("'" + T + "'" + " Is Under Construction");
                            }
                            else
                            {
                                System.out.println("'" + T + "'" + " Is working fine");                 
                            }
                          Thread.sleep(3000);
                          driver.close();
                      }
                  }
                  driver.switchTo().window(parentWindow);
            }
        }
        driver.close();

}

Aucun commentaire:

Enregistrer un commentaire