1

Building on the question asked in Selenium webdriver: How do I find ALL of an element's attributes?

When I use this answer I get the expected result of the elements attributes. However it isn't current, in other words not what I see on the screen.

For example:

>attrs = driver.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;', vlan)
>print attrs
{ u'name': u'PixF1InterfaceVLAN', u'value': u'1',}

However if I look at the same element 'vlan':

>vlan.get_attribute("value")
 u'10'

Which is what I see on the screen. My question is how can I update the Javascript code so that it shows the correct list of attributes?

Community
  • 1
  • 1
user1691282
  • 119
  • 2
  • 13

1 Answers1

4

I am making the informed guess that the element you are looking at is an input element, which has been manipulated somehow to change its value. The problem is that Selenium in trying to be helpful unhelpfully confuses the issue. Let me explain.

The JavaScript code you use does return all the values of the attributes. Here's the deal though: when an input element's value changes, it value attribute does not change but its value property does. It is the property that contains the current value of the input element. You access it as the .value field on the DOM node that corresponds to the element. You could do:

driver.execute_script('return arguments[0].value;', vlan)

to return the value of this property.

But how come vlan.get_attribute("value") works? When you execute .get_attribute("value") what Selenium does is actually get the value of the property. You think you are getting the value of the attribute, but you are not. Basically, Selenium silently corrects for the misconception that a lot of us initially have when we want to get the current value of an input element.

So you JavaScript code does exactly what it should: it return attribute values, but that's not what you need, you need the value of the value property.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • Fantastic explanation! You are spot on. Maybe you can help me one step further. My issue lies in that when I enter a certain vlan input value it causes an unexpected error. I am trying to figure out why this error is occurring without having access to the javascript source code. My thought was that maybe an additional attribute is added when it is in 'error state' that might provide a clue as to why the error is being raised on a valid input. Note this error is only raised when the value is changed via selenium and not manually updated. – user1691282 Sep 11 '15 at 13:47
  • Nothing comes to mind based on the description you give here. I would suggest posting a new question that includes the relevant Selenium code, the relevant HTML and JavaScript, and a detailed description of what you are seeing. – Louis Sep 11 '15 at 15:32