Re: How to Use selenium.isElementPresent() in selenium 2

5,905 views
Skip to first unread message

Mike Riley

unread,
Nov 20, 2012, 1:09:29 PM11/20/12
to webd...@googlegroups.com
The other option is to use findElements() and check to see if you get back an empty list.  However that searches the entire DOM, versus stopping at the first match that is found, so it is guaranteed to take the maximum amount of time all the time.

Mike

On Monday, November 19, 2012 3:38:10 AM UTC-8, Kishore K wrote:
I did a search for the same on Google as i found trail below option. Is there any other option to do apart from that...??

public bool IsElementPresent(string xpath)
{
try
{
driver.FindElement(By.XPath(xpath));
return true;
}
catch (Exception)
{
return false;
}
}

Kishore K

unread,
Nov 21, 2012, 12:28:14 AM11/21/12
to webd...@googlegroups.com
Thank you Mike for your replay ...I will check and let you know the result..

Kishore

Kishore K

unread,
Nov 21, 2012, 1:12:17 AM11/21/12
to webd...@googlegroups.com
Mike as you said findElements() is using different parameters (Like Id,Linktext etc)

In my case i'm using like this for selenium 1

if(selenium.isElementPresent(objectId)) ,here the objectId is the content ,which i'm getting frm the input Xls sheet

so how could i use this for Selenium 2 using findElements() ?



Kishore



On Tuesday, November 20, 2012 11:39:30 PM UTC+5:30, Mike Riley wrote:

Krishnan Mahadevan

unread,
Nov 21, 2012, 1:14:29 AM11/21/12
to webd...@googlegroups.com
What exactly do you mean by "objectId is the content". Can you give an example of this ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/EOZOJg9dx2QJ.
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.

Kishore K

unread,
Nov 21, 2012, 2:03:44 AM11/21/12
to webd...@googlegroups.com
Hi Krishna,

ObjectId is a parameter ,i have declared to get the content from the xls sheet based on the keyword i given .

Like if i given keyword as "LinkBText" and it will take the parameter ,which i mentioned in the xls cell and it will execute it.

For example:

I have a link and it has a name as "Flight status" in HTML ..

i will give Flight status in the ObjectId Cell and i will say keyword as Link

it will get the content and it will put in ObjectId and it will execute..Please check the trail below loop for example

else if(objectType.equalsIgnoreCase("linkBText"))
                {
                    if(selenium.isElementPresent("link="+objectId))
                    {
                       
                    driver.findElement(By.linkText(objectId)).click();


In this i want to replace IsElementPresent API with webdriver API

Kishore

Krishnan Mahadevan

unread,
Nov 21, 2012, 2:21:46 AM11/21/12
to webd...@googlegroups.com
Kishore, 

If i understand correctly looks like you need to build some sort of an intelligence in your code, that will first figure out what is the location id to be used for a given action id. Something like this :

public By identifyLocationStrategy(String objectId){
      if (objectId.beginsWith("link") return new By.ByLinkText(objectId);
      if (objectId.beginsWith("css") return new By.ByCssSelector(objectid);
// implement similarly for the rest of the methods.
}


public boolean isElementPresent(String objectId){
    By locatingStrategy = identifyLocationStrategy(objectId);
    return (driver.findElements(locatingStrategy).size > 0);
}

That should help you get your issue resolved.



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/AagJ4P8Y6CIJ.

Kishore K

unread,
Nov 21, 2012, 3:50:48 AM11/21/12
to webd...@googlegroups.com
Hi Krishna,

Thank you , Yes i did same but in different manner .

I did a Keyword driven framework and i'm using some keywords to drive the scripts .

For example if it is Text box

I will give Text as keyword in input sheet and it will mention respective parameters ( like, name of the links ,Id ,xpath) .These i'm passing these values to "ObjectId" variable.

if(objectType.equalsIgnoreCase("Text"))  ---- //** it will check for "Text" keyword,if it is text it will go in to the loop **//
                {
                    if(selenium.isElementPresent(objectId))
//** it will check for the ObjectID in the Input sheet
                    {
                        driver.findElement(By.id(objectId)).clear();
//** it will execute ,once it got the respective ObjectID



My problem is till now i'm using IsElementpresent API to check whether the element is present in the input data sheet

However ,now i want to move my script to selenium 2 for that i looking for a equivalent of IsElementpresent in webdriver.

Krishnan Mahadevan

unread,
Nov 21, 2012, 3:55:01 AM11/21/12
to webd...@googlegroups.com
Kishore,
There is no isElementPresent() API in WebDriver.

You would need to either :

a. Wrap a driver.findElement() within a try..catch. If an exception is raised, return false, else return true.
b. driver.findElements().size. If size is zero, return false, else return true.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/7-YDpRvWJ3oJ.

Kishore K

unread,
Nov 21, 2012, 4:28:09 AM11/21/12
to webd...@googlegroups.com
Thank you Krishna for your valuable help...I got that Try catch concept ,same i have mentioned in my first post it self...

Anyway i will use the same because there is no other API in webdriver

Kishore

Debabrata Chakraborty

unread,
Nov 21, 2012, 7:46:31 AM11/21/12
to webd...@googlegroups.com
You can give a try with,

ExpectedConditions.presenceOfAllElementsLocatedBy(locator) OR

ExpectedConditions.presenceOfElementLocated(locator)

 

 

To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/BLybAilofZMJ.

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.



--
Thanks,
Debabrata

fmtjatt

unread,
Nov 21, 2012, 5:51:48 PM11/21/12
to webd...@googlegroups.com
Kishore,

Try this:

List<WebElement> element = driver.findElements(By.xpath("//table[@class='lightborder']/tbody/tr")); //This will find any elements on the page with that xpath
                count = element .size();  //This will tell you how many elements are found
                System.out.println("# of element found are: "+count);
               
               if  (count>0){


                //Loop through all the found rows to search for patient
                for (i=1; i<=count; i++){
}else{
//Do something else

Kishore K

unread,
Nov 21, 2012, 11:28:59 PM11/21/12
to webd...@googlegroups.com
Thank you DC...I will try ,even Krishna suggested the same to me...

Kishore
Reply all
Reply to author
Forward
0 new messages