To copy/paste one of my previous posts, here is one way:
/**
* Function to enable us to find out if an element exists or not.
*
* @author MPLC
*
* @param String An xpath locator
* @return boolean True if element is found, otherwise false.
* @throws Exception
*/
public boolean isElementPresent(String xpathLocator) {
return isElementPresent(xpathLocator, false, "", driver);
}
/**
* Function to enable us to find out if an element exists or not and display a custom message if not found.
*
* @author MPLC
*
* @param String An xpath locator
* @param Boolean Display a custom message
* @param String The custom message you want to display if the locator is not found
* @param WebDriver the driver object to use to perform this element search
* @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 {
System.out.println("Unable to locate Element: " + xpathLocator);
}
return false;
}
return true;
}
/**
* Will wait for an element to appear on the screen, or timeout.
*
* @author MPLC
*
* @param xPathLocator Xpath of the element you are waiting for
* @param timeout time in ms to wait until returning a failure
* @param interval time in ms to wait between checks
* @return true if found, false if not
* @throws Exception
*/
public Boolean waitForElement(String xPathLocator, long timeout, long
interval) throws Exception {
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < timeout) {
Thread.sleep(interval);
if (isElementPresent(xPathLocator, true, "")) {
if
(driver.findElement(By.xpath(xPathLocator)).isEnabled()) {
return true;
}
}
}
System.err.println ("Timed out trying to locate Element: " + xPathLocator);
return false;
--
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.
--