Read more

How to click hidden submit buttons with Selenium

Arne Hartherz
January 03, 2012Software engineer at makandra GmbH

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

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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.

Posted by Arne Hartherz to makandra dev (2012-01-03 16:36)