我想通过单击页面底部的“显示更多”按钮将更多数据加载到我正在访问的页面上。
Mechanize 将这个按钮作为链接显示给我(见下文)。
根据我所做的研究,我应该能够使用下面列出的代码单击此按钮。但是,当我查看希望更长的 div 列表时,我看到计数与以前相同,这对我来说意味着某些事情没有按预期工作。
在另一个线程上,我看到按钮有时可以“伪装”为表单的一部分。我查看了页面,它有三种形式。但是,我不相信这些表单中的任何一个都与此按钮相关联,因为该按钮没有任何相关联的字段。
当我调用下面的 click 方法时,看起来点击是成功的(我可以在终端中看到某种重新加载)但 .children.count 没有改变。
#assume I already did mechanize.get(url) and I'm on the site.
#I also did a mechanize.page.links.find to get the show more button
mechanize = Mechanize.new
showMore = #<Mechanize::Page::Link "Show More" "#">
pageWithMore = mechanize.click(showMore)
# check children of div to see if count is longer
mechanize.page.css('div.results-container').children.count
# repeatedly returns 51 after 'click' invoke
我希望 .children.count 在调用 click 方法后更接近 100,然后在第二次调用后接近 150。
最佳答案
如果这是一个链接,你可以这样做:
next_page = page.link_with(text: 'Show More').click
关于ruby - 如何在网页上使用 Mechanize 到 'Show More' 单击(显然)无格式按钮/链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56467669/