splinter icon indicating copy to clipboard operation
splinter copied to clipboard

WebDriverElement.get_element_by_xpath does not search in subtree, but whole tree

Open beanqueen opened this issue 13 years ago • 7 comments

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.

beanqueen avatar Mar 15 '13 12:03 beanqueen

Could you please elaborate more?

hltbra avatar Mar 17 '13 05:03 hltbra

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.

beanqueen avatar Mar 18 '13 09:03 beanqueen

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.

hltbra avatar Mar 23 '13 15:03 hltbra

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.

dustinfarris avatar Aug 24 '14 17:08 dustinfarris

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>]

jacebrowning avatar Jul 02 '18 20:07 jacebrowning

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.

javascript-error avatar Aug 13 '19 19:08 javascript-error

@jsfehler makes sense to me. Marking this issue as invalid

andrewsmedina avatar Dec 21 '19 23:12 andrewsmedina