0

I cannot seem to find a solution on this. This line of code seems to crash if it cannot find it. Sometimes it is there and sometimes it is not:

Integer dynamicElement =  myDriver.findElements(By.xpath("//iframe[@title='CAPTCHA']")).size();

I then go on to check the value of my integer, but it crashes when it is not there. How do you get this not to crash?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • "seems to crash" - Can you be specific or can you share the error what you see on the console? Also share more code which you have tried so that you get help by the community. – Shawn Feb 27 '23 at 21:35

1 Answers1

0

findElements

findElements() finds all the elements within the current page using the given By mechanism. This method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.


An example

For a demonstration, searching for the same element on Google Search Home Page perfectly prints 0 as no WebElement is found.

  • Code Block:

    driver.get("https://www.google.com/");
    Integer dynamicElement = driver.findElements(By.xpath("//iframe[@title='CAPTCHA']")).size();
    System.out.println(dynamicElement);
    driver.quit();
    
  • Console output:

    0
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352