Skip to content

Commit a0cca3b

Browse files
committed
Adding support for buttons that live outside of their form, and instead specify a form attribute
1 parent 32f11ff commit a0cca3b

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

splinter/driver/lxmldriver.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,13 @@ def select(self, value):
439439
self._control.value = value
440440

441441
def _get_parent_form(self):
442-
parent_form = next(self._control.iterancestors("form"))
442+
# First, try to find the form by the `form` attribute.
443+
if 'form' in self._control.attrib:
444+
parent_form = self._control.getroottree().xpath(
445+
'//*[@id="%s"][1]' % self._control.attrib['form']
446+
)[0]
447+
else:
448+
parent_form = next(self._control.iterancestors("form"))
443449
return self.parent._forms.setdefault(parent_form._name(), parent_form)
444450

445451

tests/form_elements.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ def test_clicking_submit_button_posts_button_value_if_value_present(self):
8989
"submit-button: submit-button-value",
9090
)
9191

92+
def test_submitting_a_form_when_the_button_is_outside_the_form(self):
93+
self.browser.find_by_css('input[form="form-with-submit-outside"]').click()
94+
self.assertEqual(
95+
self.browser.find_by_xpath("/descendant-or-self::*").text,
96+
"submit-input: submit-input-value",
97+
)
98+
9299
def test_submiting_a_form_and_verifying_page_content(self):
93100
self.browser.fill("query", "my name")
94101
self.browser.find_by_name("send").click()

tests/static/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ <h1 id="firstheader">Example Last Header</h1>
142142
<input type="text" name="lastname">
143143
</form>
144144

145+
<form action="/post" method="POST" id="form-with-submit-outside">
146+
</form>
147+
<input type="submit" form="form-with-submit-outside" name="submit-input" value="submit-input-value" />
145148

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

0 commit comments

Comments
 (0)