how to get Xpath of all the Div elements in Selenium.

6,223 views
Skip to first unread message

santosh h s

unread,
Jun 28, 2011, 10:05:27 AM6/28/11
to seleniu...@googlegroups.com
How to get how to get  Xpath of all the Div elements in Selenium.
Suppose a div has 10 elements how can i get xpath of all the elements using selenium.

--
Regards,
Santosh

Mark Collin

unread,
Jun 28, 2011, 10:14:42 AM6/28/11
to seleniu...@googlegroups.com

You can get a list of all div elements using the following:

 

List<WebElement> bar = driver.findElements(By.xpath("//div"));

 

You can then interact with each element in the list e.g.

 

for(WebElement current : bar){

    System.out.println("Text for current element is '" + current.getText() + "'");

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

santosh h s

unread,
Jun 28, 2011, 2:24:48 PM6/28/11
to seleniu...@googlegroups.com, ma...@ardescosolutions.com
Hi,

below is my HTML code please refer the attached screen shot for the same HTML source code.

Here i want click on the list based on the text it has suppose the very first list has text San HD     and i want click on this list based on text it has .

please let me know how to achive this.

each list has different such names, so i want click based those names.
please provide sample code in Java.

<div id="discovered_list_wrapper" style="left: 114px; width: 734px;">
<ul id="discovered_sling_boxes_list" style="left: 0px; width: 13780px;">
<li class="sling_box_bg_off">
<div class="slingbox_img">
<img width="178" height="110" src="http://qawatch.slingbox.com/watch/images/sling_player/boxes/prohd.png" title="Internet Viewing">
</div>
<div class="sling_box_bottom">
<div class="box_name_with_orb">
<img width="18" height="15" src="http://qawatch.slingbox.com/watch/images/sling_player/remote_orb.png" title="Internet Viewing">
San HD
</div>
</div>
</li>
<li class="sling_box_bg_off">
<li class="sling_box_bg_off">
<li class="sling_box_bg_off off">
<li class="sling_box_bg_off off">
</ul>
</div>

/santosh 
Regards,
Santosh
screenshot.png

Omkar Khatavkar

unread,
Jun 28, 2011, 2:32:08 PM6/28/11
to seleniu...@googlegroups.com
Hi u Can Get Xpath From FireBug 

santosh h s

unread,
Jun 28, 2011, 2:47:06 PM6/28/11
to seleniu...@googlegroups.com, ma...@ardescosolutions.com
Could some one help me solving this issue. its very urgent

On Tue, Jun 28, 2011 at 11:54 PM, santosh h s <santos...@gmail.com> wrote:



--
Regards,
Santosh

tulsi.tester

unread,
Jun 28, 2011, 5:44:52 PM6/28/11
to Selenium Users
Hi santhosh,

Is all the list items have the same number of Div tags or not? If so
then the solution is here.

First you need to get the number of list items through
getXpathCount(String Xpath) method in selenium by passing the Xpath as
parameter. See below code

String Xpath = "//div[@class='discovered_list_wrapped']//ul/
li"

Number count = Selenium.getXpathCount(Xpath);
int listCount =
count.intValue(); ---->
This will give the number of list items under the UL tag. (Here i
considered there is only one UL tag)


for ( int i = 1 ; i <=listCount; i++)
{

Here is your code to click every image in that list. If you want to
check number of div tags here also use the same above method.

}


On Jun 28, 11:47 pm, santosh h s <santosh.s...@gmail.com> wrote:
> Could some one help me solving this issue. its very urgent
>
> On Tue, Jun 28, 2011 at 11:54 PM, santosh h s <santosh.s...@gmail.com>wrote:
>
>
>
> > Hi,
>
> > below is my HTML code please refer the attached screen shot for the same
> > HTML source code.
>
> > Here i want click on the list based on the text it has suppose the very
> > first list has text San HD     and i want click on this list based on text
> > it has .
>
> > please let me know how to achive this.
>
> > each list has different such names, so i want click based those names.
> > please provide sample code in Java.
>
> > <div id="discovered_list_wrapper" style="left: 114px; width: 734px;">
> > <ul id="discovered_sling_boxes_list" style="left: 0px; width: 13780px;">
> > <li class="sling_box_bg_off">
> > <div class="slingbox_img">
> > <img width="178" height="110" src="
> >http://qawatch.slingbox.com/watch/images/sling_player/boxes/prohd.png"
> > title="Internet Viewing">
> > </div>
> > <div class="sling_box_bottom">
> > <div class="box_name_with_orb">
> > <img width="18" height="15" src="
> >http://qawatch.slingbox.com/watch/images/sling_player/remote_orb.png"
> > title="Internet Viewing">
> > San HD
> > </div>
> > </div>
> > </li>
> > <li class="sling_box_bg_off">
> > <li class="sling_box_bg_off">
> > <li class="sling_box_bg_off off">
> > <li class="sling_box_bg_off off">
> >  </ul>
> > </div>
>
> > /santosh
>
> > On Tue, Jun 28, 2011 at 7:44 PM, Mark Collin <m...@ardescosolutions.com>wrote:
>
> >> You can get a list of all div elements using the following:****
>
> >> ** **
>
> >> List<WebElement> bar = driver.findElements(By.xpath("//div"));****
>
> >> ** **
>
> >> You can then interact with each element in the list e.g.****
>
> >> ** **
>
> >> for(WebElement current : bar){****
>
> >>     System.out.println("Text for current element is '" + current.getText()
> >> + "'");****
>
> >> }****
>
> >> ** **
>
> >> *From:* seleniu...@googlegroups.com [mailto:
> >> seleniu...@googlegroups.com] *On Behalf Of *santosh h s
> >> *Sent:* 28 June 2011 15:05
> >> *To:* seleniu...@googlegroups.com
> >> *Subject:* [selenium-users] how to get Xpath of all the Div elements in
> >> Selenium.****
>
> >> ** **
>
> >> How to get how to get  Xpath of all the Div elements in Selenium.****
>
> >> Suppose a div has 10 elements how can i get xpath of all the elements
> >> using selenium.
>
> >> --
> >> Regards,
> >> Santosh****
>
> >> --
> >> 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
> >> postmas...@ardescosolutions.com

Long Phuong

unread,
Jun 28, 2011, 7:40:25 PM6/28/11
to seleniu...@googlegroups.com
try: 
 
click xpath=(//img[following::text()[. = 'San HD']])[1]

then replace "San HD" with other name for other image.
 
replace 1with 2 , 3 or ... n  for multiple instances.

Date: Tue, 28 Jun 2011 23:54:48 +0530
Subject: Re: [selenium-users] how to get Xpath of all the Div elements in Selenium.
From: santos...@gmail.com
To: seleniu...@googlegroups.com; ma...@ardescosolutions.com

santosh h s

unread,
Jun 29, 2011, 1:46:22 AM6/29/11
to seleniu...@googlegroups.com
Tulsi,
all the list has same no of div

Long Phuong

unread,
Jun 29, 2011, 2:00:50 PM6/29/11
to Selenium Users
this will also work:

click xpath=(//div[. = 'San HD'])[1]


On Jun 28, 10:46 pm, santosh h s <santosh.s...@gmail.com> wrote:
> Tulsi,
> all the list has same no of div
>
> Santosh- Hide quoted text -
>
> - Show quoted text -

tulsi.tester

unread,
Jun 29, 2011, 4:44:21 PM6/29/11
to Selenium Users
Santosh,

Then the above code should work

On Jun 29, 1:46 am, santosh h s <santosh.s...@gmail.com> wrote:
> Tulsi,
> all the list has same no of div
>

Mark Collin

unread,
Jun 30, 2011, 4:09:29 AM6/30/11
to seleniu...@googlegroups.com
I'm counting at least 5 different but valid solutions so far :)

this will also work:

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

Reply all
Reply to author
Forward
0 new messages