I am trying to migrate Java code from Selenium 1 (RC) to Selenium 2 (WebDriver) that looks something like this:
1: selenium.click(someButton);
2: selenium.waitForPageToLoad();
3: if (!selenium.isElementPresent(errorMessageElement)) {
4: Assert.fail("Test failed! No error msg should be displayed on page.");
5: }
The critical part is line 3, which I tried to translate to Selenium 2 according to advice by Rostislav Matl :
3: if (!driver.findElements(By.xpath(errorMessageElement)).size() > 0) {
Unfortunately, WebDriver waits for an entire timeout (in my case 60 seconds) to detect that the element really does not exist. Although this works, it introduces way too much time overhead…
Is there any way in Selenium 2 to time-efficiently check whether an element is present or absent on the HTML page that is currently displayed in the Web browser?
here is a two part answer
The time overhead is right: As you would want to make sure that the element is really not rendered. That is, taking the page rendering time, AJAX elements etc in account. I understand that the error message would be displayed along with the page load, but timeout is useful in case you want to check presence (or absence) of ajax elements which get displayed with few milisecond delay.
Hack to reduce wait overhead: You can nevertheless create a method to temporarily reset the implicit wait time like this
and call this method where you want to check the absence, say for example the 3rd and 4th line would be