如何使用Mechanize选择表内的选项字段?

时间:2013-12-12 20:04:37

标签: html ruby opencart mechanize

我正在尝试使用Mechanize从以下html代码(由OpenCart生成)中选择<option value="20">Betaling ontvangen via Bank</option>

<div class="vtabs-content" id="tab-history" style="display: block;">
  <div id="history">
  <table class="form">
    <tbody>
      <tr>
        <td>Bestelstatus:</td>

        <td><select name="order_status_id">
          <option value="23">Bestelling geannuleerd</option>

          <option value="17">Bestelling ontvangen</option>

          <option selected="selected" value="24">Bestelling verzonden</option>

          <option value="22">Betaling mislukt</option>

          <option value="20">Betaling ontvangen via Bank</option>

          <option value="19">Betaling ontvangen via PayPal</option>

          <option value="21">Betaling via Bank mislukt</option>

          <option value="18">Betaling via PayPal mislukt</option>

          <option value="25">Gereed voor afhalen (Delft)</option>

          <option value="26">Wachten op betaling</option>
        </select></td>
      </tr>

      <tr>
        <td>Mail naar klant:<span class="help">Wel zichtbaar voor klant bij
        de bestelinfo.</span></td>

        <td><input name="notify" type="checkbox" value="1"></td>
      </tr>

      <tr>
        <td>Opmerkingen</td>

        <td>
          <textarea cols="40" name="comment" rows="8" style="width: 99%"></textarea>

          <div style="margin-top: 10px; text-align: right;">
            <a class="button" id="button-history" name=
            "button-history">Geschiedenis bijwerken</a>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</div>

我现在正在做的是:

puts single_order_page.field_with(:name => 'order_status_id').options[0].select

打印出来:

exampleshop.rb:44:in `block in <main>': undefined method `field_with' for #<Mechanize::Page:0x007f8ea514abf0> (NoMethodError)
    from /Users/narekaramjan/.rvm/gems/ruby-2.0.0-p353@global/gems/mechanize-2.7.3/lib/mechanize.rb:442:in `get'
    from exampleshop.rb:7:in `<main>'

puts single_order_page打印出页面的完整HTML代码。您可以在此处查看完整代码:http://pastebin.com/pe5pjMzB

pp single_order_page打印出此代码:http://pastebin.com/ezSWWqi0

有什么想法吗?

更新

更新订单状态的POST请求:

$('#button-history').live('click', function() {
    $.ajax({
        url: 'index.php?route=sale/order/history&token=545c8c415eb6641f29efc9e0b1180d2b&order_id=260',
        type: 'post',
        dataType: 'html',
        data: 'order_status_id=' + encodeURIComponent($('select[name=\'order_status_id\']').val()) + '&notify=' + encodeURIComponent($('input[name=\'notify\']').attr('checked') ? 1 : 0) + '&append=' + encodeURIComponent($('input[name=\'append\']').attr('checked') ? 1 : 0) + '&comment=' + encodeURIComponent($('textarea[name=\'comment\']').val()),
        beforeSend: function() {
            $('.success, .warning').remove();
            $('#button-history').attr('disabled', true);
            $('#history').prepend('<div class="attention"><img src="view/image/loading.gif" alt="" /> Even wachten a.u.b.</div>');
        },
        complete: function() {
            $('#button-history').attr('disabled', false);
            $('.attention').remove();
        },
        success: function(html) {
            $('#history').html(html);

            $('textarea[name=\'comment\']').val('');

            $('#order-status').html($('select[name=\'order_status_id\'] option:selected').text());
        }
    });
});

2 个答案:

答案 0 :(得分:0)

我会这样做:

page = single_order_page.parser

desired_element = page.search('//select[name="order_status_id"]/option[@value="20"]')

然后随便做任何你想做的事。

答案 1 :(得分:0)

这是因为field_withForm方法,而不是Page方法。

你想做类似的事情:

page = agent.get url
form = page.forms[0]
form['order_status_id'] = 20
next_page = form.submit