Clicking a button with Ruby Mechanize

后端 未结 3 691
星月不相逢
星月不相逢 2020-12-16 14:26

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:



        
3条回答
  •  独厮守ぢ
    2020-12-16 15:23

    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.

提交回复
热议问题