What is the command for waitforelementpresent in selenium 2.0

1,063 views
Skip to first unread message

NagaRaju dasam

unread,
Jun 3, 2011, 5:10:08 AM6/3/11
to Selenium Users
Hi All,

I am trying to use waitforelementpresent command in selenium2.0 ...what is the exact syntax for that?

Is there any API For selenium 2.0 like we have for seleniumRC??

--
Thanks,
Naga


Mark Collin

unread,
Jun 3, 2011, 5:19:21 AM6/3/11
to seleniu...@googlegroups.com

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.


-- 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

Daniel Wagner-Hall

unread,
Jun 3, 2011, 3:56:40 PM6/3/11
to Selenium Users
Selenium already has the waiting logic implemented in
org.openqa.selenium.support.ui.WebDriverWait, you just need to pass a
condition:

public final class Element {
public static ExpectedCondition<WebElement> isPresent(final By by) {
return new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(final WebDriver driver) {
return driver.findElement(by);
}
}
}
}

new WebDriverWait(driver, 3).until(Element.isPresent(By.id("foo")));

tulsi.tester

unread,
Jun 5, 2011, 4:09:10 AM6/5/11
to Selenium Users
@Nagaraju Dasam

The relevant command for the waitForElementPresent is
selenium.isElementPresent();

emereyd

unread,
Jun 6, 2011, 5:43:03 AM6/6/11
to Selenium Users
Daniel, thanks very much for the code snippet.
I tried that and it works a treat on FF, but on not on IE7. As soon
the the element i'm waiting for is present, my test fails with 'Unable
to find element' error.

I've tried it for a few different elements that don't immediatly load,
and have had the same result.

Suggestions/help?

E

Saranya Madhusoodanan

unread,
Jun 3, 2011, 5:34:47 AM6/3/11
to seleniu...@googlegroups.com
Hi,
You can declare & define a function 
 
public void waitForElementPresent(String element, int timeout) throws Exception {
for (int second = 0;; second++) {
if (second >= timeout)
fail("Timeout. Unable to find the Specified element" + element);
try {
if (selenium.isElementPresent(element))
break;
} catch (Exception e) {
}
Thread.sleep(1000);
}
}


 and call it with the element name & timeout (seconds)
eg: - waitForElementPresent("link=Department", 10);
--
Thanks & Regards,
Saranya M

Saranya Madhusoodanan

unread,
Jun 6, 2011, 6:09:17 AM6/6/11
to seleniu...@googlegroups.com
Hi...
That is why i used the above coding. In that there is a time delay for finding the element .  If u have time. Plz try it 


Thanks ,
Saranya M

Saranya Madhusoodanan

unread,
Jun 6, 2011, 1:08:56 AM6/6/11
to seleniu...@googlegroups.com
Hi,
Use of  selenium.isElementPresent(); is right, but here we have to catch the boolean value in a variable, then check if it is true, if so then follow a set of procedures, else print the element is not found.
E.g: 
Boolean eleValue= (selenium.isElementPresent("link=Department"));
if(eleValue==true)
{
.......
                        .......
}
else
                     System.out.println("not found");

   If we create a function using selenium.isElementPresent(); in the main class , we can call the function and can avoid repetition .It will also provide a time to check the element( in seconds)
Like :

public void waitForElementPresent(String element, int timeout) throws Exception {
for (int second = 0;; second++) {
if (second >= timeout)
fail("Timeout. Unable to find the Specified element" + element);
try {
if (selenium.isElementPresent(element))
break;
} catch (Exception e) {
}
Thread.sleep(1000);
}
}




--
Thanks & Regards,
Saranya Madhusoodanan

poobathy r

unread,
Jun 15, 2011, 2:18:25 PM6/15/11
to seleniu...@googlegroups.com
Saranya

      Thanks  It worked for WaitforTextPresent() also

---Poobathy

--

Saranya Madhusoodanan

unread,
Jun 18, 2011, 11:57:23 AM6/18/11
to seleniu...@googlegroups.com
Poobathy,

 :) .. K

--
Thanks,
Saranya Madhusoodanan


poobathy r

unread,
Jun 20, 2011, 1:59:05 AM6/20/11
to seleniu...@googlegroups.com
But You  have to Use try catch while calling the function otherwise it  might cause the Exception.......


 

Reply all
Reply to author
Forward
0 new messages