How to use a WebElement in CSS or XPath Expression?

365 views
Skip to first unread message

driverlearner

unread,
Mar 16, 2013, 8:22:52 AM3/16/13
to seleniu...@googlegroups.com
Hi All,

Suppose a WebElement is webElement. I wanna use webElement in CSS or XPath Expression for identifying other element/elements.

When we write webElement.findElements(...), does it mean find all the immediate child elements or find all the descendants of webElement?(I have checked it and it is the later one)

If I wanna find the some elements which are on the same level as webElement, how to do that?

How can I use this weElement  in XPath and CSS expressions so that it would make it easier to find other elements?

Example:

Consider the following snippet of html:

<td id="unique">
</td>
<td>
</td>
<td>
</td>

I have webElement which represents the first td element.

 I can simply have this by WebElement webElement = driver.findElement(By.id("unique"));

Now I would like to use this webElement to identify the 2nd and 3rd td elements. Any help is greatly appreciated.

Thanks,



ash

unread,
Mar 16, 2013, 6:09:22 PM3/16/13
to seleniu...@googlegroups.com
You need to learn how to identify element in the table via css selector or xpaths.

Go through this site http://plasmasturm.org/log/444/ which might give you some help.

Hope that helps!






--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/bdxZ_-1f6ncJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


 

Mike Riley

unread,
Apr 2, 2013, 6:09:54 PM4/2/13
to seleniu...@googlegroups.com
It will find all the child elements.  However, using XPath you can also find siblings by using "../" notation for the relative path.

I often need to do that when I am able to identify a specific row element and then want to use it to access all the other elements in the row of a table.

Mike

SantoshSarma

unread,
Apr 3, 2013, 3:16:42 AM4/3/13
to seleniu...@googlegroups.com
I think you can not use WebElement as part of another locator (XPath, Css).

driver.findElements(By.xpath/css) will return List<WebElement> of webelements which has same locator in that page.

In your case if you have same id for all td s (<td id="unique">) then you can use below logic to access each individual td

List<WebElement> allTds=driver.findElements(By.id("unique"))
for(WebElement eachTd:allTds)
{
     
System.out.println(eachTd.getText());
}

Mike Riley

unread,
Apr 3, 2013, 4:35:18 PM4/3/13
to seleniu...@googlegroups.com
You think wrong.  I do it all the time when working with table elements.

When you use a WebElement it uses that as the starting point and basically only searches for elements underneath it.  The exception is if you use XPath to climb to elements above or at the same level of the DOM as that element.

Mike

SantoshSarma

unread,
Apr 4, 2013, 12:55:56 AM4/4/13
to seleniu...@googlegroups.com
Thanks Mike for correcting me and useful info in this regard. Will try that.

Mike Riley

unread,
Apr 4, 2013, 4:21:21 PM4/4/13
to seleniu...@googlegroups.com
The web site I was testing was all tables and usually I could only search for one of the columns by value or ID.  So using XPath relative addressing was great.  I often created a method to return the content of a particular column and all you did was pass any WebElement for that row.

Using relative XPath was quite fast when done that way, because it didn't have to do the search from the beginning of the DOM.

Mike

David Lai

unread,
Apr 4, 2013, 11:50:24 PM4/4/13
to seleniu...@googlegroups.com
I observed that when you pass WebElements into an executeScript() call, they are automatically converted into JavaScript objects of the HTML nodes they map to.  Interesting trick I've found you can do this using a bit of jQuery injection is use a hybrid of jquery and seleniums electors. 

jqueryFile = open("/path/to/jquery.js")
driver.executeScript(jqueryFile.read())

parent_div = driver.find_element_by_id("parentDivID")

target_element = driver.execute_script("$(argument[0]).find('li').filter(function(){return $(this).text=='some text';}).siblings('a')")
target_element.click()

I found this a pretty interesting technique for selecting complicated elements which normally would take an xpath 2.0 or css 3 expression, which would break often in IE8, but was much more reliable using jquery injection.


Reply all
Reply to author
Forward
0 new messages