0

Is there a way to print all the info about a web element?

When I call a print on a web element that I did retrieve via find_element_by_css_selector(), I get the internal ID and another reference number, but not all the properties.

For example, I would like to know the class name, the source and alt if it is an image; the label if it is a button and so on.

I did use get_attribute(); but that is useless if you do not know the name of the attribute; it just return empty if the attribute does not exist, and I need to check an element to know which attributes it has.

Is there a way to get these attributes from the webelement?

1 Answers1

0

You can't directly, you need to execute Javascript. See Selenium webdriver get all the data attributes of an element.

In python, it's like:

 >>> el = browser.find_element_by_id('my_element')
 >>> attrs = browser.execute_script("var items = {}; for (index=0; index<arguments[0].attributes.length;++index) {items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;", el)
 >>> print attrs
 {'class': 'my_class', 'name': 'foobar'}
Community
  • 1
  • 1
pbuck
  • 4,291
  • 2
  • 24
  • 36