0

What is the best way that element doesnt exist in page. for example TEST - This will appear when there is data other wise this link wont be there.

How to identify this quickly because I had issue Global timeout ? The global Wait for element hold me on the page for long time

is there any way quckily identify that?

JJJ
  • 32,902
  • 20
  • 89
  • 102
joe
  • 34,529
  • 29
  • 100
  • 137
  • do you read question before -1 – joe Aug 25 '17 at 14:49
  • Before you use the explicit wait as suggested in answers you should set the implicit global timeout to zero. Else you may have lots of fun debugging combination of implicit and explicit waits. Later set it back to original value. You could even try using List instead of WebElement and check for size. – Grasshopper Aug 25 '17 at 15:34
  • When the page is loading every time do we need to thread rather than implicit ? – joe Aug 25 '17 at 15:43
  • What do you mean by thread? – Grasshopper Aug 25 '17 at 15:57

3 Answers3

0

Try to locate the element with findElement. If the element doesn't exist you will receive an exception. Based on this you can decide what you want to do next.

try {
        WebElement element = driver.findElement(By.id(<element selector string>)
    }
catch(NoSuchElementException nse)
 {}
Amit G
  • 186
  • 1
  • 11
0

Assuming that you don't know whether there will be data or not i would still use a wait, even if it's for a second or 2 and just catch the exception and do not as previously suggested

try {
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.Until(ExpectedConditions.ElementIsVisible(By.Id("elementId")));

    element = driver.findElement(By.Id("elementId");
} catch (NoSuchElementException | TimeoutException) {}

Of course if when you hit the page you already know that whether the link should be there or not and there's no need to wait for anything to load then yes as previously suggested catch the exception and do nothing with it.

so cal cheesehead
  • 2,515
  • 4
  • 29
  • 50
  • This doesn't work because if the element is not there, it will throw a timeout exception which you haven't caught. – JeffC Aug 25 '17 at 17:28
  • You're right but in that case he could simple add another catch block. Now i understand that adding an insane amount of catch blocks is not ideal but if it's only those 2 or simple the timeout exception then it should still work, also assuming he's using java 7+ – so cal cheesehead Aug 25 '17 at 18:57
  • Best practice would be to use `findElements()` (plural) and check for an empty list returned. That way you can avoid all the exceptions. – JeffC Aug 25 '17 at 21:50
  • @JeffC perhaps I'm missing something here but while I would agree that checking a list is a good approach it will fail in the event the link hasn't loaded yet. It would need to be in a loop with a sleep statement which would go back to using a wait. Again unless I'm missing something and when he hits the page the link is either there or not then yes checking an empty list would do the trick. – so cal cheesehead Aug 25 '17 at 23:56
  • OP didn't say anything about waiting. He said a quick check. Making sure that a list is or isn't empty is a quick check. The problem with waits is that he's expecting it to not be there... so how long do you wait for something to exist that isn't expected to exist. Yes, you could throw in a wait but as I said, best practice is to use `findElements()` per the docs. – JeffC Aug 26 '17 at 00:28
  • [The docs](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#findElement-org.openqa.selenium.By-). `findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead.` – JeffC Aug 26 '17 at 00:29
0

You can use following function for that, it will store all elements in a list and if the size is greater than true element is present:

public boolean isElementPresent(By Locator){

List<WebElement> allItems  = driver.findElements(locator);

if (allItems.size() >0){

return true;
}else{

return false

}

}
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46