当前位置: 动力学知识库 > 问答 > 编程问答 >

web - Emulate a HTML <button> press within Ruby

问题描述:

There's a website that I want to scrape (FWIW it's svpply) and there's a button that shows up sometimes saying "Show All", it's an HTML <button> element. Is there any way to use Ruby to emulate a click of this button, and get the content of the full page that results after clicking that button, since the button reveals more content?

网友答案:

The "Show All" button triggers a javascript ajax request. The only way to automate that is to use a library that can execute javascript. Libraries such as Mechanize and ScrAPI will not work.

What will work are tools that drive an actual browser such as watir and selenium. I installed watir-webdriver and successfully got it to click the button and show additional products.

require 'watir-webdriver'

b = Watir::Browser.new
b.goto 'svpply.com/editors_pick'

#count products
puts b.elements(:xpath => '//li[@data-class="Product"]').count
#=> 30

#Now click button
show_all = b.button(:id => "btn_all")
show_all.click
sleep 4

#count products again
puts b.elements(:xpath => '//li[@data-class="Product"]').count
#=>60
网友答案:

Mechanize can do this nicely for you.

网友答案:

If the page is reloaded when you press the button, you can, otherwise you need something that can parse javascript. If you understand where the website redirects you after pressing the button (even on the same page but with some parameters set, use firebug for this purpose), you can eventually read what you need.

分享给朋友:
您可能感兴趣的文章:
随机阅读: