Skip to content

Commit

Permalink
Adding support for buttons that live outside of their form, and inste…
Browse files Browse the repository at this point in the history
…ad specify a `form` attribute
  • Loading branch information
theunraveler committed Sep 11, 2020
1 parent 32f11ff commit a0cca3b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion splinter/driver/lxmldriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,13 @@ def select(self, value):
self._control.value = value

def _get_parent_form(self):
parent_form = next(self._control.iterancestors("form"))
# First, try to find the form by the `form` attribute.
if 'form' in self._control.attrib:
parent_form = self._control.getroottree().xpath(
'//*[@id="%s"][1]' % self._control.attrib['form']
)[0]
else:
parent_form = next(self._control.iterancestors("form"))
return self.parent._forms.setdefault(parent_form._name(), parent_form)


Expand Down
7 changes: 7 additions & 0 deletions tests/form_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def test_clicking_submit_button_posts_button_value_if_value_present(self):
"submit-button: submit-button-value",
)

def test_submitting_a_form_when_the_button_is_outside_the_form(self):
self.browser.find_by_css('input[form="form-with-submit-outside"]').click()
self.assertEqual(
self.browser.find_by_xpath("/descendant-or-self::*").text,
"submit-input: submit-input-value",
)

def test_submiting_a_form_and_verifying_page_content(self):
self.browser.fill("query", "my name")
self.browser.find_by_name("send").click()
Expand Down
3 changes: 3 additions & 0 deletions tests/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ <h1 id="firstheader">Example Last Header</h1>
<input type="text" name="lastname">
</form>

<form action="/post" method="POST" id="form-with-submit-outside">
</form>
<input type="submit" form="form-with-submit-outside" name="submit-input" value="submit-input-value" />

<a href="http://example.com/">Link for Example.com</a>
<a href="http://example.com/last">Link for Example.com</a>
Expand Down

0 comments on commit a0cca3b

Please sign in to comment.