Get arrays of values from a xpath

7,216 views
Skip to first unread message

Aleksandar Nikolovski

unread,
May 9, 2011, 8:11:04 AM5/9/11
to seleniu...@googlegroups.com
Hi All, 

I am new to selenium so need some help from you gues. I am using selenium with JUnit (eclipse)

I have a xpath that returns multiple items. 
In eclipse I can do: String ItemAddress = selenium.getText( xpath goes here )

But getText returns a string, and will return only the first element. I need a method that returns array. Is selenium supporting this?

Thanks

Aleksandar Nikolovski

unread,
May 9, 2011, 8:47:55 AM5/9/11
to seleniu...@googlegroups.com
I found a workaround for this issue:
First step count the items that xpath is returning using method getXpathCount. 
Second step modify the xpath to get the exact element from the list:

Example:
XPath 
/html/body/div[2]/div[2]/div/div[2]/div[4]/div[2]/div/ul/li/div[2]/h2/a
is returning all elements
XPath 
//div[contains(@class,'result-left')]/ul/li[1]/div[2]/h2/a
is returning only one element, the element that will be returned will depend on the li[ number ] you have entered. 

But if any of you have simpler solution I would like to see it. 

Lutfi Dughman

unread,
May 9, 2011, 2:26:01 PM5/9/11
to seleniu...@googlegroups.com
selenium will catch the first element that qualifies then return its value;

give this a try though im not sure it works

put in a loop assuming you know how many are there


String ItemAddress = selenium.getText( "xpath=(yourxpath)["+ i +"]" )


i saw this solution here on SeleniumUsers group, but not sure if it will work for you.

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

Mark Collin

unread,
May 11, 2011, 8:44:39 AM5/11/11
to seleniu...@googlegroups.com

Use the selenium 2 implementation:

 

List<WebElement> itemAddressList = driver.findElements(By.xpath("//foo"));

 

This will give you a list of webElements that you can iterate through as required.

 

Regards

 

Mark

--

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

Krishna chaitanya

unread,
May 17, 2011, 3:53:29 PM5/17/11
to selenium-users
Use getXpathCount and find the number of entries which u get using the xpath . Now use a loop and use the solution given by dughman selenium.getText( "xpath=(yourxpath)["+ i +"]" )  and form an array with them ..

Mark Collin

unread,
May 18, 2011, 6:10:01 AM5/18/11
to seleniu...@googlegroups.com

This won’t work unless your xpath is counting the children of a specific parent.

Mohammed Sikander

unread,
May 18, 2011, 7:31:42 AM5/18/11
to seleniu...@googlegroups.com
Hi,

Since you need all the values in an array, try following. The following method returns you a String array.

public String[] getItemAddress(){
String[] itemAddress = null;
int getCount = selenium.getXpathCount("//xpath").intValue();
for(int 1=1; i<=getCount; i++){
for(int j=0; j<getCount; j++){
itemAddress[j] = selenium.getText("//table/tbody/tr["+i+"]/td[1]/a")
}
return itemAddress; 
}

hope this helps you..

--

Mark Collin

unread,
May 18, 2011, 8:19:16 AM5/18/11
to seleniu...@googlegroups.com

Again you are assuming that the OP is looking for elements that are all the child of the same parent.

 

Solutions like this would not work in the following case:

 

<html>

<head>

</head>

<body>

<div>

<ul class=”foo”>

<li>stuff</li>

                                                </ul>

</div>

<div>

<ul class=”foo”>

<li>other stuff</li>

                                                </ul>

</div>

<div>

<ul class=”foo”>

<li>not stuff</li>

                                                </ul>

</div>

</body>

</html>

 

An xpath count of //ul[@class=”foo”] on the above would return a count of 3, however every element above is an //ul[@class=”foo”][1] (i.e. the first child of a parent) so your iteration solution below cannot and will not work.

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Mohammed Sikander
Sent: 18 May 2011 12:32
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Get arrays of values from a xpath

 

Hi,

Marcus Döring

unread,
May 18, 2011, 2:13:29 PM5/18/11
to Selenium Users
selenium.getText( "xpath=(yourxpath)["+ i +"]" )
confirmed working-

but you should notice that this method is extremely slow. it might be
acceptabe for 1-100 iterations, but then, it just gets out of hand and
you will notice a performance impact.

a quicker way to solve this would be to implement this funktionality
yourself (through the userextension.js) javascript functions executed
through selenium always return a string, so you just need so append
the javascript results in astring and split it in your tests.

On 9 Mai, 20:26, Lutfi Dughman <lutf...@gmail.com> wrote:
> selenium will catch the first element that qualifies then return its value;
>
> give this a try though im not sure it works
>
> put in a loop assuming you know how many are there
>
> *String ItemAddress = selenium.getText( "xpath=(yourxpath)["+ i +"]" )*
> *
> *
> *
> *
> ***i saw this solution here on SeleniumUsers group, but not sure if it will
> work for you.
> *
> On Mon, May 9, 2011 at 8:11 AM, Aleksandar Nikolovski <morko...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Hi All,
>
> > I am new to selenium so need some help from you gues. I am using selenium
> > with JUnit (eclipse)
>
> > I have a xpath that returns multiple items.
> > In eclipse I can do: *String ItemAddress = selenium.getText( xpath goes
> > here )*

Mark Collin

unread,
May 19, 2011, 3:47:53 AM5/19/11
to seleniu...@googlegroups.com
I refer yet again to my previous answer:

Marcus Döring

unread,
May 20, 2011, 12:47:40 AM5/20/11
to Selenium Users
Sorry, but I dont see why selenium.getText( "xpath=(yourxpath)["+ i
+"]" ) would not work in your example.
Aleksandar wanted to get an array of text from all elements matching a
specified XPath.
So, for your example with selenium.getText( "xpath=("//
ul[@class=”foo”]/li")["+ i +"]" ) you would get "stuff" / "other
stuff" and "not stuff" ("i" beeing 1, 2 and 3)
which, for me, seems to be correct.
> 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

Mark Collin

unread,
May 20, 2011, 1:26:05 AM5/20/11
to seleniu...@googlegroups.com
No you won't, try it if you don't believe me.

In the example below each <li> element is the first child of its parent <ul>
element, if you look for the second child (// ul[@class=”foo”]/li[2]) you
won't find anything because there are no second children under an <ul>
element.

You are actually proving my point :)

If you have received this email in error please notify postm...@ardescosolutions.com

Krishna chaitanya

unread,
May 20, 2011, 3:18:46 AM5/20/11
to selenium-users
Mark collin is right in the example given by him . Quite a number of times , there are html's with common classes which leads to wrong xpathcount . But these cases are less . Once can check his html source if he has that class name used more number of times and work on that . And as specified by marcus , there will be an performance impact getting the xpath count and iterating it . Writing your own JavaScript extension would work too.

I feel Selenium2 webdriver answer is the best fit as of now 
Message has been deleted

Aleksandar Nikolovski

unread,
May 20, 2011, 10:42:22 AM5/20/11
to seleniu...@googlegroups.com
This is what I need. Will try this.

Thanks
Mark

Aleksandar Nikolovski

unread,
May 20, 2011, 10:45:14 AM5/20/11
to seleniu...@googlegroups.com
Thanks all for you're posts. 

My idea works but I wanted to know if there was an option to do this without first getting the count and then go through them with a for.
Mark give an interesting option. I am not sure if it will work but I will try it.

Marcus Döring

unread,
May 20, 2011, 5:24:09 PM5/20/11
to Selenium Users
wait a second.
it's not "(yourXPath["+i+"])"
it is "(yourXPath)["+i+"]"
What this does is, it treats all results which match this expression
like if it were an array (starting with 1)

i tested it with this site. I wanted to get the names of the authors.

the html for this part looks in every post like this:

<span class="fontsize2 author">
<span style="color: #5B1094;">Author</span>
</span>

count(//span[@class='fontsize2 author']/span)
returns 15 (16 when my post here got added)
and if do now (//span[@class='fontsize2 author']/span)[1] to (//
span[@class='fontsize2 author']/span)[15] I get the names otf the
authors.
> For more options, visit this group athttp://groups.google.com/group/selenium-users?hl=en.

Mark Collin

unread,
May 21, 2011, 9:20:26 AM5/21/11
to seleniu...@googlegroups.com
Ah I missed the second parenthesis, in that case my apologies you are
correct, that will work :)

If you have received this email in error please notify postm...@ardescosolutions.com

Reply all
Reply to author
Forward
0 new messages