XPath For Returning a Column Index

1,721 views
Skip to first unread message

KevinO

unread,
Dec 9, 2011, 4:16:08 PM12/9/11
to Selenium Users
I have the following html:

<html>
<head>
<body>
<table>
<tr>
<td>first column</td>
<td>second column</td>
</tr>
<tr></tr>
</table>
</body>

The xpath expression for the first row second column is:

/html/body/table/tbody/tr/td[2]
I'd like to use selenium to get the value '2'.

In other words, what is the value for the first occurrence of text
"second column". I'm able to get selenium to recognize that element
but not return the value.

Mark Collin

unread,
Dec 12, 2011, 5:31:08 AM12/12/11
to seleniu...@googlegroups.com
What you are asking for is not he value from an HTML point of view, the
value attribute of the element will be null.

What exactly are you trying to achieve? If the test is to confirm that you
are looking at the second column you have already done that with the XPath
you have supplied.

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

KevinO

unread,
Dec 12, 2011, 11:09:01 AM12/12/11
to Selenium Users
Here is the solution I came up with. Thanks for the help.

http://stackoverflow.com/questions/8448828/xpath-for-returning-the-column-index

> For more options, visit this group athttp://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 postmas...@ardescosolutions.com

Hien Ngo

unread,
Dec 12, 2011, 12:15:43 PM12/12/11
to seleniu...@googlegroups.com
Interresting. I wonder how do you find out the end index, or you just iterate until you see exception out of array boundary?

Mark Collin

unread,
Dec 12, 2011, 1:11:35 PM12/12/11
to seleniu...@googlegroups.com

Finding the number of children an element has is very easy:

 

List<WebElement> childrenOfAnElement = driver.findElements(By.id("myElement"));

int numberOfElements = childrenOfAnElement.size();

 

You need to keep in mind that if you are iterating through the elements using XPath as described in the accepted solution in the link the XPath’s will start at 0 so you need to decrement the size by one to make sure you don’t get a null pointer error.


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

Mark Collin

unread,
Dec 13, 2011, 3:48:40 AM12/13/11
to seleniu...@googlegroups.com

Finding the number of children an element has is very easy:

 

List<WebElement> childrenOfAnElement = driver.findElements(By.id("myElement"));

int numberOfElements = childrenOfAnElement.size();

 

You need to keep in mind that if you are iterating through the elements using XPath as described in the accepted solution in the link the XPath’s will start at 0 so you need to decrement the size by one to make sure you don’t get a null pointer error.

 

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Hien Ngo


Sent: 12 December 2011 17:16
To: seleniu...@googlegroups.com


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