使用ruby mechanize导航结果页面(链接已定义“onclick =”)

时间:2016-09-01 05:29:26

标签: ruby pagination mechanize watir

需要使用ruby mechanize导航结果页面的帮助。我能够提交表单值并下载第一页的结果。链接到下一页是

 <a href=# onclick="setnext();">Next</a> 

如何单击下一步并导航到后续结果页面。我试过了

 page = r_page.link_with(:text => 'Next').click

并使用watir

@browser = Watir::Browser.new
@login_page = result_page
@l_page = @login_page.link(:text => "Next").fire_event('onclick') 
@browser.goto @l_page

我收到网址

的错误消息
<a href=# onclick="setnext();">Next</a> 

未正确形成。我也尝试使用page_no作为参数调用页面,但它加载表单提交页面而不是结果页面。请帮助。 谢谢你。

1 个答案:

答案 0 :(得分:0)

The point is, mechanize won't interprete the JavaScript function that should be executed when the button is clicked (onclick="setnext();").

You have at least two options:

  1. Read and understand the JavaScript code (or debug through it).
  2. Use the tools your browser ships with (e.g. Developer in Mozilla Firefox, usually accessible by pressing F12, Chromium ships with a very similar thing), head over to the network tab and inspect the traffic that actually happens. You will see a GET/POST request to a specific URL (with some interesting parameters?) when you click the button/link and probably are able to figure out how the URL and the parameters are constructed.

As I am lazy, understand enough HTTP and do not yet enjoy reading JS code enough, I usually use the second approach.

Because mechanize will not interprete the JavaScript out of the box, I switch off JavaScript in browser first and see if the page works. If it doesnt, I look at the generated requests (with JS switched on again).