From 6aaa47cd0ef746d3abd2933865c93f9c5fb275b3 Mon Sep 17 00:00:00 2001 From: Danielle Madeley Date: Wed, 17 Sep 2014 13:09:11 +1000 Subject: [PATCH] Fix find_button to find the exact match for the button Including only normalising the space. This also means we only make one XPath request. --- lettuce_webdriver/util.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lettuce_webdriver/util.py b/lettuce_webdriver/util.py index 302c576..53dec68 100644 --- a/lettuce_webdriver/util.py +++ b/lettuce_webdriver/util.py @@ -66,10 +66,21 @@ def field_xpath(field, attribute): def find_button(browser, value): - return find_field_with_value(browser, 'submit', value) or \ - find_field_with_value(browser, 'reset', value) or \ - find_field_with_value(browser, 'button', value) or \ - find_field_with_value(browser, 'image', value) + input_types = ( + 'submit', + 'reset', + 'button', + 'image', + ) + + xpath = '|'.join( + '//input[@type="{type_}" and normalize-space(@value)="{value}"]'.format( + type_=type_, value=value) + for type_ in input_types) + + xpath += '|//button[normalize-space(text())="{value}"]'.format(value=value) + + return browser.find_element_by_xpath(xpath) def find_field_with_value(browser, field, value):