Equivalent of selenium.isElementPresent

9,580 views
Skip to first unread message

Arya

unread,
Jan 15, 2010, 3:26:02 PM1/15/10
to webdriver
Hello

I'm trying to check if certain text is on the webpage, I used to do it
with selenium.isElementPresent(), but how would I do it with
Webdriver?

Best Regards!

Jason Leyba

unread,
Jan 15, 2010, 3:52:53 PM1/15/10
to webd...@googlegroups.com
WebDriver.findElement() will throw a NoSuchElementException if the
element is not on the page. You can use this to test for an element's
presence:

public static boolean isElementPresent(WebDriver driver, By by) {
try {
driver.findElement(by);
return true; // Success!
} catch (NoSuchElementException ignored) {
return false;
}
}

If you want to wait for an element to be present, use
org.openqa.selenium.support.ui.WebDriverWait:

WebElement element = new WebDriverWait(driver, 5000)
.until(new Function<WebDriver, WebElement>() {
return driver.findElement(By.id("foo"));
});

> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>
>
>
>

JerraraJohn

unread,
Jan 17, 2010, 6:01:10 PM1/17/10
to webdriver
RenderedWebElement element = (RenderedWebElement) driver.findElement
(By.name("value of name attribute"));

if(element.isDisplayed()){
dislayed = true;
}

On Jan 16, 7:52 am, Jason Leyba <jmle...@gmail.com> wrote:
> WebDriver.findElement() will throw a NoSuchElementException if the
> element is not on the page. You can use this to test for an element's
> presence:
>
> public static boolean isElementPresent(WebDriver driver, By by) {
>   try {
>     driver.findElement(by);
>     return true;  // Success!
>   } catch (NoSuchElementException ignored) {
>     return false;
>   }
>
> }
>
> If you want to wait for an element to be present, use
> org.openqa.selenium.support.ui.WebDriverWait:
>
> WebElement element = new WebDriverWait(driver, 5000)
>     .until(new Function<WebDriver, WebElement>() {
>         return driver.findElement(By.id("foo"));
>     });
>

emjot

unread,
Jan 18, 2010, 5:37:37 AM1/18/10
to webdriver
I think this solution is Suboptimal.

Not only that is it not that easy likle with the old selenium to use
the return of isElementPresent() in an "if" condition, but exception
tend to have a bad performance, so I use the old selenium API with
WebDriverBackedSelenium for this task...

greetings,
Martin

Jason Leyba

unread,
Jan 18, 2010, 12:18:46 PM1/18/10
to webd...@googlegroups.com
Hi,

WebDriver is built on the expected use case. If you search for an
element on the page, then it's an exceptional condition for the
element not to be found - hence the NoSuchElementException. If you
really want to use the result in an if statement, you can add a helper
function like I showed above.

With respect to performance, the cost of throwing the exception is
minimal and it could only make a real difference if you're doing a
waitForIsElementPresent-like operation. In which case, the cost of
repeatedly sending the findElement command will far out weigh throwing
the exception.

Regards,

Jason

Reply all
Reply to author
Forward
0 new messages