如何使用 while 扫描所有页面以使用 Ruby 或 Capybara 查找特定记录

How to use a while to scan all pages looking for a specific record with Ruby or Capybara

我听说水豚只有在页面上可见时才会找到它。 ruby是否可以扫描所有页面寻找这条记录?

我尝试了下面的解决方法:但是根据朋友Thomas的帮助,只有在列表中可见的项目才能找到Capybara,如果在其他分页中,Capybara找不到

@ger_material_active = 'automation_Server_ALUMINIO_ACTIVE'+ rand(1..99).to_s find("td", 文本: @ger_material_active).click

是的,您可以使用循环遍历所有分页响应,直到找到您要查找的文本。为此,您必须知道在页面上单击什么以移动到下一页结果,并且选择器指示结果页面已完成加载或为每个页面设置足够长的等待时间负载

until page.has_css?("td", text: @ger_material_active, wait: 3) do
  # You need to click whatever moves to the next page of results - I can't tell what that would be because you haven't provided any page structure/HTML
  click_button('next result') # this is not correct, it's just an example - you need to click whatever moves to the next page of results
end
find("td", text: @ger_material_active).click

这是最简单的方法,但也是最慢的方法,因为您需要将 wait 值设置得足够大,以确保每次都加载下一组结果。检测表明结果为 loading/loaded 的页面更改会更有效,而不是仅仅等待元素出现几秒钟,但是告诉您如何做到这一点将需要更多有关该元素的信息页面及其工作原理(哪些元素 appear/disappear、状态变化等)。这也有可能会造成无限循环,除非您确保编写的用于单击结果的下一页按钮的代码意识到它在最后一页并且不只是继续尝试单击以获取新的页面结果。