How to get the xpath of the elements during the run time....

1,333 views
Skip to first unread message

Elangovan Ganesh

unread,
Nov 14, 2011, 7:35:54 AM11/14/11
to Selenium Users
Hi All,

The scenario is

While i am submitting the form with the client code 123, it will
display list of 5 hyperlinked links. i need to click each link and
have to automate further...

then if i submit the form with the client code 124, it displays list
of 7 different hyperlinked links. as same i need to process all these
links.

so in this case i need to fetch the hyperlinks (xpath) dynamically i.e
during runtime. based on the client code am providing.

How to do?. Can anyone help me?

Thanks,
Elangovan G

Mark Collin

unread,
Nov 14, 2011, 8:32:46 AM11/14/11
to seleniu...@googlegroups.com
The main question is why do you need the XPath?
What are you trying to do?

If you did manage to get an XPath it would be an absolute one that will be
very brittle, it will also become invalid if the DOM changes and is probably
not going to be that much use to you.

Hi All,

The scenario is

Thanks,
Elangovan G

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

haotten

unread,
Nov 14, 2011, 10:57:42 AM11/14/11
to Selenium Users
I agree with Mark.... you shouldn't be asking yourself how to get the
xpath of an element, you should be using xpath to define a pattern
that will return all of your elements. A great link for understanding
xpath is http://www.w3schools.com/XPath/default.asp.

Basically, you should look at the html that is returned when you
"click" on your button, and identify the xpath that will return all
the items you are interested in. For instance if, after clicking on
your button, the page is updated, and your html looks like this :

<div id='myResults'>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>


Then, using WebDriver.findElements method, the following java code
would get your list of items (no matter how many are returned).

//Asume that "driver" has already been initialized, let's get all the
li items.
List<WebElement> elements = driver.findElements(By.xpath("//
div[@id='myResults']/ul/li")); //See link above to better understand
xpath.

//Now loop through the list and display each element
for (WebElement element : elements) {
System.out.println(element.getText());
}


Hope that helps.
> 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,
Nov 14, 2011, 11:39:27 AM11/14/11
to seleniu...@googlegroups.com
Looking back at the original post I assume you are trying to do something
like this:

List<WebElement> linksFound = driver.findElements(By.id("foo"));
for(WebElement link : linksFound){
link.click();
// Check you are where you expect to be
driver.navigate().back();

Elangovan Ganesan

unread,
Nov 15, 2011, 4:10:41 AM11/15/11
to seleniu...@googlegroups.com
yeah it ll be fine if you try to get the items(label name) of the
dropdown list dynamically.

But mine problem is different.

let me explain clearly,

there is a dropdown list named State. Based upon the state am
selecting it ll display some of the types with each radio button.

i want to capture all the radio button,s xpath/something to give to
selenium.click("radio button")

am using selenium RC, not a webdriver.

i have attached the page source. save it with html extension. see my
requirements...

It is not like the dropdown values present under the same tag <ul>...</ul>.

Thanks,
Elangovan.G

page.txt

Pushpraj Singh

unread,
Jun 18, 2014, 7:51:47 AM6/18/14
to seleniu...@googlegroups.com
Have you tried partial xpath, something like this:

private static void nmandate() {
WebDriver driver = new FirefoxDriver();
driver.get(AUT);
/*
*
*I know this page (AUT) has few items those start with img tag and has value image stored in id
*actual relative xpath if I use firebug would be //*[@id='myProfileImage']
* but I knew only that it is stored in img with id value = Image(partially)
*Later for loop will give text of all such elements and then write some code to click on them
*/
List<WebElement> list = driver.findElements(By
.xpath("//img[contains(@id,'Image')]"));



for (WebElement webElement : list) {
System.out.println("inside for loop");
System.out.println(webElement.getText());
}

}
If you get success, please share the piece of code that worked for you.
apart from contains, you can use starts-with and ends-with
Reply all
Reply to author
Forward
0 new messages