how to get title element from anchor tag (<a .... </a>) by Selenium

10,207 views
Skip to first unread message

Ripon Al Wasim

unread,
Oct 27, 2011, 12:31:58 AM10/27/11
to seleniu...@googlegroups.com
Dear All,

I have the following snippet code:

<a id="4ea70e73ff0cae84ee82de1f" class="inactiveIndicator" href="javascript:;" title="Inactive">My Group-Ripon</a>

How can I get the value of "title" element? What's the xpath of the above code?

I am using Selenium RC with Java. I am also using TestNG as Framework.

br,
Ripon

Mark Collin

unread,
Oct 27, 2011, 1:44:07 AM10/27/11
to seleniu...@googlegroups.com

A working xpath based on the html provided would be:

 

//a[@id=’4ea70e73ff0cae84ee82de1f’]

 

In webdriver world you would be looking at the following:

 

String titleAttribute = driver.findElement(By.xpath(“//a[@id=’4ea70e73ff0cae84ee82de1f’]“)).getAttribute(“title”);

 

I suspect however that the ID is randomly generated and you’ll need something a little more involved than the above.

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

Jayakumar C

unread,
Oct 27, 2011, 4:00:00 AM10/27/11
to seleniu...@googlegroups.com
For Se-RC:

selenium.getAttribute("//a[@id='4ea70e73ff0cae84ee82de1f']@title")

The general form of the method is getAttribute("locator@attribute")

You need to have some other locator, if the id is dynamic.
Jayakumar

Ripon Al Wasim

unread,
Oct 27, 2011, 4:11:53 AM10/27/11
to seleniu...@googlegroups.com
Thanks for the quick reply. It's my fault not to mention ID is dynamic. So it's better not to use id.

br,
Ripon

Mark Collin

unread,
Oct 27, 2011, 5:32:57 AM10/27/11
to seleniu...@googlegroups.com

So how exactly are we supposed to you an accurate xpath when you have given us 1 element from the page with an invalid ID? 

 

I mean seriously?

 

Let me just read your mind for you and magically construct the html using a mixture of black magic, ritual sacrifice and my amazing telepathy skills…

 

Got it, the answer you want is now in my mind, use your own telepathy skills to get it!

 

(apologies if the above is a bit OTT but come on how are we supposed to answer anything without any information?)

Arun Dev @ work

unread,
Oct 27, 2011, 6:22:07 AM10/27/11
to seleniu...@googlegroups.com

Following is the HTML for the icon - adchoice icon. i need to test it.
as you see the id - "te-bab2-anch-mo" is randomly generated. so i am not
able specify anything stable, any ideas ?

Firebug:-
<span id="te-bab2-anch-mo" style="font-weight: normal; font-family:
arial,sans-serif; font-size: 8pt; text-decoration: none; display:
none;"><span style="line-height:15px;
vertical-align:top;"><span
style="font-size:8pt;font-weight:normal;text-
transform:none;color:#000;margin: 0 13px 0 0;"></span><img
width="77px" height="15px" style="border:none;position:absolute;right:
0px;top:0;" src="http://choices.truste.com/assets/ad_choices_en.png"></
span></span>




Error message when verifying

[error] Element link=[close] not found

Mark Collin

unread,
Oct 27, 2011, 12:44:36 PM10/27/11
to seleniu...@googlegroups.com

*sigh*

 

An XPath is a representation of a journey down your DOM.  If we don’t have a map of the DOM, we can’t tell you how to navigate down it to your required destination.

 

What you are asking us to do is direct you from India to Britain when you are only giving us a map of London…

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Arun Dev @ work
Sent: 27 October 2011 11:22
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] how to get title element from anchor tag (<a .... </a>) by Selenium

 

--

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.

Ripon Al Wasim

unread,
Oct 31, 2011, 1:13:29 AM10/31/11
to seleniu...@googlegroups.com
Hello Mark,

You are right. Anyway, by the help of your comments, I have got it. I have solved it by the following way:

selenium.getAttribute("//a[@class='inactiveIndicator']@title");

Thanks again.,

br,
Ripon Al Wasim

bis

unread,
Oct 31, 2011, 1:17:10 AM10/31/11
to seleniu...@googlegroups.com
I think this is right for in ruby.

driver.find_element(:tag_name => 'a').attribute('title').text

Ripon Al Wasim

unread,
Oct 31, 2011, 2:09:54 AM10/31/11
to seleniu...@googlegroups.com
thanks all, for your quick feedback. I have solved it by help of you.

br,
Ripon
Reply all
Reply to author
Forward
0 new messages