0

cannot print values from the merged lists as it is throwing me null!

List<WebElement> DateTime2 = driver.findElements(By.xpath(""));
driver.findElement(By.xpath("")).click();
List<WebElement> DateTime3 = driver.findElements(By.xpath(""));
List<WebElement> DateTime = new ArrayList<>(DateTime2);
DateTime.addAll(DateTime3);
Thread.sleep(2000);
System.out.println("This is for testing the list " + 
DateTime.get(2).getText());
System.out.println("This is for testing the list " + 
DateTime.get(30).getText());

i expect output to be printed date and time

6
  • whats the size of DateTime2 & DateTime3? Commented Aug 7, 2019 at 14:09
  • you should post the full error so I know where the exception is being thrown from Commented Aug 7, 2019 at 14:10
  • @Sureshmanithe size for DateTime2 is 19 and DateTime3 is 19 Commented Aug 7, 2019 at 15:33
  • @randypaq13 the exception is thrown at DateTime.get(2).getText()); j Caused by: org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=75.0.3770.142) (Driver info: chromedriver=71.0.3578.137 (86ee7228 Commented Aug 7, 2019 at 15:36
  • findElements will return when at least 1 object is found. Since you are getting "staleElement Exception" from Selenium, it's very possible that the list is still populating via client side script when you attempt to grab the elements. You need to put this in a function, use a WebDriverWait and try/catch the stale element exception... if caught, re-run the function until stale element is no longer thrown (or after a certain number of tries... it'll poll the DOM every 1/2 second.) Commented Aug 7, 2019 at 17:19

1 Answer 1

0

Here's an example of some code I use (though I changed it a bit for this example. This is usually a part of a class that extends another.)

int sanitycount= 0;
int ec_Timeout = 10; //seconds to wait for list... 
    public void RunAction(WebDriver driver, String in_xpath)
    {
                  try
      {
        wait = new WebDriverWait(driver, ec_Timeout);
        List<WebElement> found_elements = new ArrayList<>();
        found_elements = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(in_xpath)));

        if (!found_elements.isEmpty())
        {
        // store this array, return an array from function
        // or loop through array and add items to more global array..
        }

       }
       catch (Exception e)
       {
       if (e.getClass().getCanonicalName().equals("org.openqa.selenium.StaleElementReferenceException"))
         {
         //need to do it again, not finished loading
         System.out.println("*****************Stale caught-redoing");
         sanitycount++;
         if (sanitycount<ec_Timeout * 2)
         {
         RunAction(driver, in_xpath);
         }
      System.out.println (e.toString());
      }
      else
      {
      System.out.println (e.toString());
      }
    }
  }
RunAction(your_driver, your_xpath); 
//set sanitycount back to zero if you run again...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.