2

I need to check if some element exists on the page or not.
I already saw this WebDriver: check if an element exists? issue but I'm wondering why not simply apply findElements().isEmpty method?
I thought it would do the same work.

UPD Now I see findElements().isEmpty works perfect so I'm just wondering why to look for other, much complex ways, while there is a straightforward method for that?

Community
  • 1
  • 1
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • The accepted answer suggests `driver.findElements( By.id("...") ).size() != 0` which is exactly the same as `!driver.findElements( By.id("...") ).isEmpty()`. What is your question, again? – Petr Janeček Jun 23 '15 at 13:51
  • If so why no one mentioned `isEmpty` method (at least I didn't see that), only the `size` method is used for that? Additionally I tried to apply `isEmpty` method but so far I see it doesn't work as I thought. – Prophet Jun 23 '15 at 14:00
  • 2
    They mentioned it, in the comments below the answer. So, yes, it's generally a better practice to use `isEmpty()` instead of `size() == 0`, and static analysis tools will even point that out as a warning. You're right, `isEmpty()` was the better choice. Under the hood, it does the same thing, though. – Petr Janeček Jun 23 '15 at 14:03

5 Answers5

2

isEmpty() is actually from the Java List class, as findElements() returns a List of WebElements.

Prophet
  • 32,350
  • 22
  • 54
  • 79
aholt
  • 2,829
  • 2
  • 10
  • 13
  • 2
    ...and that's exactly why the `driver.findElements(selector).isEmpty()` should be preferred. It's shorter and avoids using exceptions for flow control which is a good practice overall. – Petr Janeček Jun 23 '15 at 14:02
0

findElements returns a list of all elements matching a given selector. So you are using java's list built in isEmpty() method here.

Cathal
  • 1,318
  • 1
  • 12
  • 19
  • 2
    checking for visibility != checking for existence. – aholt Jun 23 '15 at 13:55
  • yep, true. I didnt say it was though or it wasnt asked – Cathal Jun 23 '15 at 13:56
  • 1
    the original question doesn't say anything about checking for visibility. It is asking about checking for existence. Just clarifying. With your example, if it does not exist, an exception will get thrown. – aholt Jun 23 '15 at 14:00
  • nope, your right it doesnt. Butit doesnt ask either to check if an element exists. Eliyahu asks what isEmpty() is, which i answered in the first line. – Cathal Jun 23 '15 at 14:02
  • I asked "I wish to check if some element exists on the page or not." and only after that I asked "So what is isEmpty used for and how?" :) – Prophet Jun 23 '15 at 14:06
0
 public static WebElement findElement(WebDriver driver, By selector, long timeOutInSeconds) {
    WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
    wait.until(ExpectedConditions.presenceOfElementLocated(selector));
    return findElement(driver, selector);
  }


  public static WebElement findElementSafe(WebDriver driver, By selector, long timeOutInSeconds) {
    try {
      return findElement(driver, selector, timeOutInSeconds);
    } catch (TimeoutException e) {
      return null;
    }
  }
Ran Adler
  • 3,587
  • 30
  • 27
  • With Google Guava and/or Java 8, your `findElementSafe()` method might be better of using the `Optional` class as a return value instead of a potential `null`. – Petr Janeček Jun 23 '15 at 14:19
  • 2
    Anyway, while you added a fairly nice answer to "how to check if an element exists", you didn't answer OP's question _"So what is isEmpty used for and how?"_ – Petr Janeček Jun 23 '15 at 14:20
0

The below code works fine in case of the WebDriver

if(driver.findElements(By.xpath(xpath)).size()!=0)
{
    ...
}

But if we use WebElementFacade we can write some thing like below which is pretty much easier.

@FindBy(xpath = "xpathvalue")
private WebElementFacade element;

if(element.isPresent())
{
    ...
}
mnille
  • 1,328
  • 4
  • 16
  • 20
Seshadri
  • 1
  • 3
  • Why do you think this works better than simplest findElements().isEmpty as I mentioned in the topic? – Prophet Jun 27 '16 at 16:50
  • if we use the WebElementFacade we don't have to get the driver which is required to call the findElements() method. – Seshadri Jun 28 '16 at 13:21
  • Well, but I even do not know what WebElementFacade is. I will read about it later. Thanks! – Prophet Jun 28 '16 at 16:29
0

The best way that I found by limiting the scope of search for elements in the page for example this function search for any tag Name you want to get the text of those tags and store it in an ArrayList then all what you have to do is to match the element text that you want to prove not exist in the page with the ArrayList values. -if the element text not found in the array that means your test Pass: element not exist/display.. in the page

/**
 * @params : tagName, elementText
 * @desc: To check the Elements is not Exist in the page - by actual tagName for element and the expected element text
 */
public void verifyElementIsNotExistByTagNameAndElementText(String tagName, String elementText) {
    ArrayList<String> arrList;
    List<WebElement> listOfTags = driver.findElements(By.tagName(tagName));
    arrList = listOfTags.stream().map(WebElement::getText).collect(Collectors.toCollection(ArrayList::new));
    System.out.println(ConsoleColors.BLACK_BOLD+"All TagNames Text: "+arrList+ConsoleColors.RESET);
    if (!arrList.contains(elementText)) {
        System.out.println(ConsoleColors.GREEN_BOLD+"PASS: "+ConsoleColors.BLACK_BOLD+elementText+ConsoleColors.BLACK+" is not exist in the page"+ConsoleColors.RESET);
    } else {
        System.out.println(ConsoleColors.RED_BOLD+"FAIL: "+ConsoleColors.BLACK_BOLD+elementText+ConsoleColors.BLACK+" is exist in the page"+ConsoleColors.RESET);
    }
}
  • I can't see how this answers my question I asked many years ago? BTW now I can answer and describe this and many other my question from that period much better.... – Prophet Nov 08 '21 at 09:46