How to click hidden submit buttons with Selenium

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