This is a migrated thread and some comments may be shown as answers.

How to get the attribute value by its name?

4 Answers 319 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Juan
Top achievements
Rank 1
Juan asked on 09 May 2012, 04:13 PM
Hi everyone,

As you know, we can get the attribute value by the index, but there is a way to get the attribute by the "attribute name" instead of the Index? Because the index is not the same in the different browsers.

I've tried it, but nothing :(

Could someone please help me to see if this is possible?
Thanks,
Juan 

4 Answers, 1 is accepted

Sort by
0
Anthony
Telerik team
answered on 09 May 2012, 09:28 PM
Hello Juan,

What type of element are you trying to locate? Is it HTML, Silverlight, or WPF?

Here is a basic example for an HTML Anchor. By using Find.ByExpression you can use any attribute you like:

HtmlAnchor a = Find.ByExpression<HtmlAnchor>("tagname=a", "href=//en.wikipedia.org/");

See here for more common HTML Find Expressions and here for more in-depth information.

Kind regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Juan
Top achievements
Rank 1
answered on 10 May 2012, 03:07 PM
Hi Anthony,

Basically I want to get the attributes by the "attribute name". Example:

I have a link with the text: "Go to Google", so I want to get its attribute by the name, no by the index:

?myManager.ActiveBrowser.Find.ByAttributes("Go to Google").Attributes
Count = 3
    [0]: {(Attribute name='lang', value='en')}
    [1]: {(Attribute name='class', value='google_link')}
    [2]: {(Attribute name='xmlns', value='http://www.google.com')}

As you can see, I have three attributes for that element. Right now, I'm able to get the attribute value by the attribute index:

?myManager.ActiveBrowser.Find.ByAttributes("Go to Google").Attributes[2].Value
"http://www.google.com"

My question is: Is there a way to get the attribute value by the attribute name? Something like this:

?myManager.ActiveBrowser.Find.ByAttributes("BMW").Attributes["xmlns"].Value
"http://www.google.com"

Does my question make sense?
Thanks,
Juan

0
Accepted
Anthony
Telerik team
answered on 10 May 2012, 08:06 PM
Hello Juan,

Given the following HTML:

<a lang="en" class="google_link" xmlns="http://www.google.com">Go to Google</a>

I accessed individual attributes from the element with the following code:

Element e = Find.ByTagIndex("a", 0);
Assert.IsNotNull(e);
 
Log.WriteLine(e.Attributes.Single(a => a.Name == "lang").Value);
Log.WriteLine(e.Attributes.Single(a => a.Name == "class").Value);
Log.WriteLine(e.Attributes.Single(a => a.Name == "xmlns").Value);

And here's the log output:

LOG: en
LOG: google_link

Greetings,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Juan
Top achievements
Rank 1
answered on 11 May 2012, 05:46 PM
That is exactly that I was found :)
Thanks a lot Anthony!
Tags
General Discussions
Asked by
Juan
Top achievements
Rank 1
Answers by
Anthony
Telerik team
Juan
Top achievements
Rank 1
Share this question
or