I want to check that text is now not present on a webpage without my test failing
I have achieved this by catching the exception but is there a better way of doing this?
try {
selenium.isTextPresent(selenium.getText("27"));
} catch (Exception e) {
System.out.println("Element does not exhist");
}
There’s nothing wrong with try-catch.
Anyway, what you have should be more of a
isElementPresent("27")since you discard the return value ofisTextPresent(). Moreover, if the element exists, thenisTextPresent()will always return true, because … well, you took the text out of existing element, it has to be there. In this case, it is enough just to assure whether the element exists or not.But if you do actually need it in real code somehow, then what about
if (selenium.isElementPresent("27") && selenium.isTextPresent(selenium.getText("27")))?Also,
getXpathCount(//*[@id='27' and text()]) == 0expression does the trick, too. It count the number of returned elements that have id=27 and contain some text. If that’s a zero, there are none.