How to click hidden submit buttons with Selenium

Updated . Posted . Visible to the public.

In your Cucumber features you can't really click hidden elements when using Selenium (it does work for a plain Webrat scenario, though).

Unfortunately you need to hack around it, like this:

When /^I press the hidden "([^\"]+)" submit button$/ do |label|
  page.evaluate_script <<-JS
    $('input[type=submit][value="#{label}"]').show().click();
  JS
end

If your button is nested into a container that is hidden this will not do the trick. You need a more complex method to also show surrounding containers:

When /^I press the hidden "([^\"]+)" submit button$/ do |label|
  page.evaluate_script <<-JS
    (function() {
      var button = $('input[type=submit][value="#{label}"]');
      button.parents(':hidden').show();
      button.show().click();
    })();
  JS
  And "I wait for the page to load"
end

You may want to wait for the page content to load afterwards. You should be able to do it by saying: page.has_content? ""

Keep in mind that the above examples use jQuery. Tweak accordingly if you are using another JavaScript framework.

Arne Hartherz
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2012-01-03 15:36)