Extract an Individual HTML Attribute

PROBLEM

I would like to extract an individual HTML attribute and use it later in the test.

SOLUTION

This is possible with a coded solution. First, be aware that if you're simply verifying the value for a specific attribute, that can be accomplished without code using an Advanced Verification.

 

HTML elements are formatted the following way:

 

<tagname attribute="value">content</tagname>

 

We'll use the following HTML in this example:

 

<a href="http://www.google.com" lang="en" id="googleLink">Go to Google</a>

 

Here's how to set the value of the lang attribute (which is en) to a string. That string is then set as an extracted value to use later in the test through data binding (either attached to an input value or a verification).

 

C#

HtmlAnchor a = Find.ById<HtmlAnchor>("googleLink");
Assert.IsNotNull(a);
 
string atr = a.Attributes.Single(x => x.Name == "lang").Value;
Log.WriteLine(atr);
SetExtractedValue("extraction", atr);

 

Visual Basic

Dim a As HtmlAnchor = Find.ById(Of HtmlAnchor)("googleLink")
Assert.IsNotNull(a)
 
Dim atr As String = a.Attributes.[Single](Function(x) x.Name = "lang").Value
Log.WriteLine(atr)
SetExtractedValue("extraction", atr)