WebDriverElement.get_element_by_xpath does not search in subtree, but whole tree
I have a couple of select boxes in my html file:
<select name="nr_select_name" id="nr_select_1" >
<option value="option 1">text 1</option>
<option value="option 2">text 2</option>
<option value="option 3">text 3</option>
</select>
<select name="nr_select_name" id="nr_select_2" >
<option value="option 1">text 1</option>
<option value="option 2">text 2</option>
<option value="option 3">text 3</option>
</select>
<select name="nr_select_name" id="nr_select_3" >
<option value="option 1">text 1</option>
<option value="option 2">text 2</option>
<option value="option 3">text 3</option>
</select>
<select name="nr_select_name" id="nr_select_4" >
<option value="option 1">text 1</option>
<option value="option 2">text 2</option>
<option value="option 3">text 3</option>
</select>
I would like to be able to first select a select box, then select an option by text (i.e. "text 1" in select box with id "nr_select_4")
After the first search I get a WebDriverElement as in:
'ele': <splinter.driver.webdriver.firefox.WebDriverElement object at 0x10c1a2650>
then I do this search: ele.find_by_xpath('//option')
which should give me a list of 3 options.
It gives me a list that includes all options in the whole page.
Based on what is says here: http://splinter.cobrateam.info/docs/finding.html#chaining-find-of-elements I think it does not behave properly.
Could you please elaborate more?
Thank you for your quick answer, hltbra. I'm sorry, but I don't really know how to be more elaborate. I'll try it with code.
foo = splinter.Browser()
single_select = foo.find_by_id('nr_select_3')
This should give you this element:
<select name="nr_select_name" id="nr_select_3" >
<option value="option 1">text 1</option>
<option value="option 2">text 2</option>
<option value="option 3">text 3</option>
</select>
Next - within the found element - I want to do a xpath search:
options_in_single_select = single_select.find_by_xpath('//option')
there should be 3 options.
assert(len(options_in_single_select), 3) -> fail
There were 14, though. And in my html file, there are 14 options all in all.
I hope, this helps.
Now it makes sense to me, and other people have requested this same behavior before.
Great you mentioned it, @beanqueen. I will think about it and maybe write a patch.
Just ran into this myself. Was there ever a verdict on fitting this in? Probably easier to hack a solution on a case-by-case basis than to patch splinter.
This also seems to affect methods that use xpath under the hood.
On my site, there are three "Sign Up" buttons on a particular page:
>>> browser.find_by_text("Sign Up")
[<splinter.driver.webdriver.WebDriverElement at 0x105e17978>,
<splinter.driver.webdriver.WebDriverElement at 0x105e17908>,
<splinter.driver.webdriver.WebDriverElement at 0x105e17c88>]
But only one of the buttons is inside the login-form div, yet they are all returned:
>>> browser.find_by_css(".login-form").find_by_text("Sign Up")
[<splinter.driver.webdriver.WebDriverElement at 0x105e19550>,
<splinter.driver.webdriver.WebDriverElement at 0x105e195c0>,
<splinter.driver.webdriver.WebDriverElement at 0x105e19978>]
This is actually the correct implementation of the xpath spec, not a bug. See: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/403
A leading // will search relative to the document root, not the current context. ./ is required to search within the context.
@andrewsmedina In regards to Splinter's find_by_text method, Element.find_by_text will search within the child, not from the root, so there's nothing to fix here.
@jsfehler makes sense to me. Marking this issue as invalid