Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in DevOps and Agile by (19.7k points)

In the Python Selenium module, once I have a WebElement object I can get the value of any of its attributes with get_attribute():

foo = elem.get_attribute('href')

If the attribute named 'href' doesn't exist, None is returned.

My question is, how can I get a list of all of the attributes that an element has? There doesn't seem to be a get_attributes() or get_attribute_names() method.

I'm using version 2.44.0 of the Selenium module for Python.

1 Answer

0 votes
by (62.9k points)

By using IJavaScriptExecutor, you can get all the unknown attributes of a web element. HAP has already “attributes” property for HtmlNode object but compared to Selenium, HAP hasn’t any option to handle DOM objects or CSS related properties.

ReadOnlyCollection<object> attributes = (ReadOnlyCollection<object>) 

 javascriptDriver.ExecuteScript(

      @"console.log(arguments[0].attributes);"+

      @"var _return = []; var attrList = arguments[0].attributes; " +

      @"for (var _index = 0; _index < attrList.length;++_index)" +

      @"{ var _attr = attrList[_index];var _node ={ } ;"+

      @"_node.name = _attr.name;_node.value = _attr.value;" +

      @"_return.push(_node); "+

      "}; return _return; "

      , item);

The above code is a basic example to retrieve all attributes of an IWebElement. You can run any javascript code with ExecuteScript command within C# on web driver.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium certification online!

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...