5

Using Selenium webdriver I have the following element

<div data-1223="some data" data-209329="some data" data-dog="some value">

Is there a way to get all the data- attributes of the element? For a specific attribute I can use

elem.getAttribute('data-1223') 

but how can I get all the data attributes together

uri wald
  • 328
  • 1
  • 5
  • 9

3 Answers3

17

Use below Code:-

WebElement element = "Your Element"; // Your element
JavascriptExecutor executor = (JavascriptExecutor) driver;
Object aa=executor.executeScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;", element);
System.out.println(aa.toString());

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • 2
    Don't forget to check that your driver is a `JavascriptExecutor` before casting to it :) Several driver classes do not implement this. – Paul Hicks Apr 05 '16 at 04:18
  • @PaulHicks *Several driver classes do not implement this* Fair enough to do the check, though I can't think of any impls that don't implement this...? All 11 in my proj do. – Andrew Regan Apr 05 '16 at 22:22
  • Any driver that wraps SimpleBrowser doesn't, which is pretty obvious, since SimpleBrowser doesn't support JavaScript. Also, the `EventFiringWebDriver` doesn't, though it does give you access to `WrappedDriver`, which almost always does. – Paul Hicks Apr 05 '16 at 23:25
  • Matter of interest: you shouldn't need 11 drivers. If you have to deal with that many browsers, consider delegating everything to `RemoteWebDriver` and letting Selenium look after that complexity. – Paul Hicks Apr 05 '16 at 23:27
  • @ShubhamJain What if I only wanted the attribute names without the values? The JS you provided returns both the names and the values.. – Kevin Little Mar 22 '17 at 14:25
2

No. There is no way in Selenium WebDriver to access any attribute whose full name you don't know. You can't even enumerate over all attributes of a WebElement.

It doesn't look like the jsonwire protocol supports this concept. The GET_ELEMENT_ATTRIBUTE command takes a list of attribute names, not a list of attribute name patterns.

It is probable that you could query via a JavascriptExecutor and either XPath or CSS query to find all the attribute names for your element, then use that to loop over getAttribute. But if you're going to do that, you can just construct your XPath or CSS query to give you the values directly, and leave the WebElement out of it completely.

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
1

This concept works very well. Try this!

WebElement element = driver.findElement(By.tagName("button"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
Object elementAttributes = executor.executeScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;",element);

System.out.println(elementAttributes.toString());
fractals
  • 816
  • 8
  • 23