Finding next input element using Mechanize?

后端 未结 3 977
粉色の甜心
粉色の甜心 2021-01-20 22:00

Using Mechanize, is it possible to find a phrase in the HTML of a page, for example, \"email\", and find the next after that, and fill in that input

3条回答
  •  攒了一身酷
    2021-01-20 22:26

    First find the element with the phrase text:

    el = page.at('*[text()*="some phrase"]')
    

    From there you can get the first following input:

    input = el.at('./following::input')
    

    Now, find the ancestor form node of that input:

    form_node = input.ancestors('form')[0]
    

    Then use that to get the Mechanize::Form object

    form = page.form_with(:form_node => form_node)
    

    And now you can fill out the value

    form[input[:name]] = 'foo'
    

提交回复
热议问题