Check If an Object/Element exists

1,280 views
Skip to first unread message

Manojith

unread,
Jun 24, 2011, 7:12:13 AM6/24/11
to Selenium Users
Hi,
Using Selenium 2 how can we find if an object/Element is present?
likewise is QTP we have something like
Browser(...).Page(...).WebElement().Exists(1)

Regards,
Manojith

ei0haro

unread,
Jun 24, 2011, 8:14:07 AM6/24/11
to Selenium Users
Hi!
I have created an extension method that I use for this.

public static bool ElementExists(this IWebDriver webDriver, By
searchBy)
{
return webDriver.FindElements(searchBy).Any();
}

then I can call

webDriver.ElementExists(By.Id("someId"));

Mark Collin

unread,
Jun 24, 2011, 8:30:33 AM6/24/11
to seleniu...@googlegroups.com
/**
* Find out if an element exists or not.
*
* @param String - An xpath locator
* @param WebDriver - The driver object to use to perform this element
search
* @param Boolean - Display a custom message if set to true
* @param String - The custom message you want to display if the locator
is not found
* @return boolean - True if element is found, otherwise false.
* @throws Exception
*/
public boolean isElementPresent(String xpathLocator, Boolean
displayCustomMessage, String customMessage, WebDriver browserController) {
try {
browserController.findElement(By.xpath(xpathLocator));
} catch (org.openqa.selenium.NoSuchElementException Ex) {
if (displayCustomMessage) {
if (!customMessage.equals("")) {
System.out.print(customMessage);
}
} else {
logger.debug("Unable to locate Element: " + xpathLocator);
}
return false;
}
return true;
}

Regards,
Manojith

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


--
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

If you have received this email in error please notify postm...@ardescosolutions.com

Reply all
Reply to author
Forward
0 new messages