From a0cca3b0299e864c1a90420e88a5f2da480f8be9 Mon Sep 17 00:00:00 2001 From: Jake Bell Date: Fri, 14 Sep 2018 00:41:13 -0500 Subject: [PATCH] Adding support for buttons that live outside of their form, and instead specify a `form` attribute --- splinter/driver/lxmldriver.py | 8 +++++++- tests/form_elements.py | 7 +++++++ tests/static/index.html | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/splinter/driver/lxmldriver.py b/splinter/driver/lxmldriver.py index 1876ca859..2a97c6375 100644 --- a/splinter/driver/lxmldriver.py +++ b/splinter/driver/lxmldriver.py @@ -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) diff --git a/tests/form_elements.py b/tests/form_elements.py index 046323a0d..38962cb41 100644 --- a/tests/form_elements.py +++ b/tests/form_elements.py @@ -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() diff --git a/tests/static/index.html b/tests/static/index.html index 6371b9943..2354c1525 100644 --- a/tests/static/index.html +++ b/tests/static/index.html @@ -142,6 +142,9 @@

Example Last Header

+
+
+ Link for Example.com Link for Example.com