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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)