15

I have a particularly difficult form that I am trying to click the search button and can't seem to do it. Here is the code for the form from the page source:

<input type="image" name="" src="http://images.example.com/WOKRS53B4/images/search.gif" align="absmiddle" border="0" onclick="return check_form_inputs('UA_GeneralSearch_input_form','search');" title="Search" alt="Search" class="">

I am trying to do the standard mechanize click action:

login_page = agent.click(homepage.link_with(:text => "Search"))

Is this because the button uses javascript? If so, any suggestions?

Sean
  • 2,891
  • 3
  • 29
  • 39

3 Answers3

39

I struggled with this too, especially since my form had multiple buttons.

There are multiple ways to submit a form (with many using a 'form_with' block), but this helped me:

# get the form
form = agent.page.form_with(:name => "my-form")
# get the button you want from the form
button = form.button_with(:value => "Search")
# submit the form using that button
agent.submit(form, button)

See more info here

Also, make sure you upgrade to the latest mechanize. I was using mechanize 1.x, which was giving me "undefined method" errors for the code above.

Flaviu
  • 6,240
  • 4
  • 35
  • 33
4

It is not a link, it is a button. What you need to do is look for the form (for example, with form_with) and then look for the ImageButton and submit it.

Serabe
  • 3,834
  • 19
  • 24
  • Do you have some code that demonstrates how to use an image input to submit a form? – Kris Dec 12 '11 at 20:46
  • 1
    I think you should consider asking a proper question since it doesn't seem related to this one. – Serabe Dec 18 '11 at 20:40
0
button = form.button_with(value: 'Search')
form.click_button(button)
Lane
  • 4,682
  • 1
  • 36
  • 20