12

I have been testing using Selenium WebDriver and I have been looking for an XPath code to get the value of the attribute of an HTML element as part of my regression testing. But I couldn't find a good answer.

Here is my sample html element:

<div class="firstdiv" alt="testdiv"></div>

I want to get the value of the "alt" attribute using the XPath. I have an XPath to get to the div element using the class attribute which is:

//div[@class="firstdiv"]

Now, I am looking for an XPath code to get the value of the "alt" attribute. The assumption is that I don't know what is the value of the "alt" attribute.

6
  • 3
    You won't be able to do that with Selenium. Selenium expects XPath queries to return physical DOM elements. Why doesn't a getAttribute('alt') not work?
    – Arran
    Commented Apr 30, 2014 at 17:54
  • @Arran Because when you're running remote Selenium (like Browserstack), it takes ages: every getAttribute is += 2 * latency. Commented Jun 1, 2017 at 13:33
  • Agreed @polkovnikov.ph, when running Remote Selenium you should do all you can to make your tests less chatty but in this instance, there is just no other way than two calls - one to get the element and one to get an attribute from it.
    – Arran
    Commented Jun 1, 2017 at 15:13
  • @Arran Take a look at my solution down there with 1 call. Commented Jun 1, 2017 at 19:06
  • @polkovnikov.ph, sure, if you are happy doing it that way, great! :)
    – Arran
    Commented Jun 2, 2017 at 14:29

4 Answers 4

18

You can use the getAttribute() method.

driver.findElement(By.xpath("//div[@class='firstdiv']")).getAttribute("alt");
1
  • thanks Richard for the quick response... however, my case is that i have this application wherein user will just provide me the xpath and based on it the system will do the testing...but i guess there is no other way but do two calls...get the element first and then get the attribute
    – arn-arn
    Commented Apr 30, 2014 at 18:48
3

Using C#, .Net 4.5, and Selenium 2.45

Use findElements to capture firstdiv elements into a collection.

var firstDivCollection = driver.findElements(By.XPath("//div[@class='firstdiv']"));

Then iterate over the collection.

        foreach (var div in firstDivCollection) {
            div.GetAttribute("alt");
        }
1

Just use executeScript and do XPath or querySelector/getAttribute in browser. Other solutions are wrong, because it takes forever to call getAttribute for each element from Selenium if you have more than a few.

  var hrefsPromise = driver.executeScript(`
        var elements = document.querySelectorAll('div.firstdiv');
        elements = Array.prototype.slice.call(elements);
        return elements.map(function (element) {
              return element.getAttribute('alt');
        });
  `);
1

Selenium Xpath can only return elements. You should pass javascript function that executes xpaths and returns strings to selenium.

I'm not sure why they made it this way. Xpath should support returning strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.