3

I have a page with an element:

<input id="foo"></input>

which (via Javascript) has had its value set to "bar".

If I use the following XPath statement (with Python/Selenium):

self.browser.find_elements_by_xpath("//input[@id='foo']")

it returns my element, and I can even do:

self.browser.find_elements_by_xpath("//input[@id='foo']")[0].get_attribute("value")

and get u"bar" back. However, if I try to do:

self.browser.find_elements_by_xpath("//input[@value='bar']")

or even just:

self.browser.find_elements_by_xpath("//input[@value]")

my element doesn't get returned.

The only way this makes sense to me is if my Javascript code is setting the "value" property, not the "value" attribute (and when I look at the element in the Firefox inspector it doesn't show a "value" attribute, which reinforces this theory).

So, my question is, how do I select elements with a "value" property instead?

machineghost
  • 33,529
  • 30
  • 159
  • 234
  • Shouldn't `self.browser.find_elements_by_xpath("//input[@value='foo']")` actually be `self.browser.find_elements_by_xpath("//input[@value='bar']")`? – Richard Jan 14 '14 at 17:58
  • Er yes (sorry messed up when replacing real names with fake); editing to fix. As you can see though that's not the actual problem, as even [[@value]` fails – machineghost Jan 14 '14 at 18:01
  • you don't have your input like that in your app do you? `` elements need self-closing tags. they are not block elements. it should be written like `` – ddavison Jan 14 '14 at 18:41
  • 1
    Run your XPath directly in [Chrome](http://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome) or [Firefox](http://getfirebug.com/wiki/index.php/$x) => what do you get? – Arran Jan 14 '14 at 18:47
  • @sircapsalot I just copied that text from out of the Firefox inspector; the element is generated by JS so there's never even any HTML to forget the `/` on. – machineghost Jan 14 '14 at 19:26
  • @arran I'm running the XPath via a Selenium/Python method; I'm not clear on how I'd run it inside the browser (without installing some kind of XPath plug-in or something, which is difficult because Selenium runs without plug-ins). – machineghost Jan 14 '14 at 19:27
  • @machineghost, that's why I linked you to two places in my comments ;) The first one will tell you how to do it in Chrome and the second is how to do it in Firefox. The aim here is to bypass Selenium altogether and check what *exactly* the browser is returning from the XPath querying you are sending it, *outside* of Selenium. – Arran Jan 14 '14 at 19:46
  • @Arran The Firefox XPath thing fails to include the input in `//input[@value]` also .. but then it does return "bar" for `$x("//input[@id='foo']")[0].value`. So it's consistent with Selenium. – machineghost Jan 14 '14 at 22:04
  • You say "_which (via Javascript) has had its value set to "bar"_", but exactly how do you do that? Because that's going to be related to the difference between a DOM attribute and a JavaScript property. – Ross Patterson Jan 14 '14 at 23:13
  • Via `$.('#foo').val('bar');` and yeah I suspected that the issue was that I'm setting a property not an attribute, but that just brings us back to my question "how do I select elements with a "value" property instead?". – machineghost Jan 14 '14 at 23:38

1 Answers1

2

As you've apparently figured out, you're not really interested in the [@value] which is the 'value' attribute on the DOM object. Instead you're trying to find the value of the "foo" property on the JavaScript object.

Normally properties for JavaScript objects aren't exposed on the DOM. You'd need to query down into the JavaScript itself to look at those objects and their properties. You can do that via the JavaScript executor. I've not used the Python bindings for WebDriver, so the exact syntax/approach may vary a bit--but the approach is generally the same.

Jim Holmes
  • 1,680
  • 8
  • 10