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

Time:05-27

I've been told that capybara will only find it if it's visible on the page. Is it possible with ruby ​​to scan all pages looking for this record?

I tried the solution below: But according to the help of my friend Thomas, Capybara can only be found if the item is visible in the list, if it is in another pagination, Capybara cannot find it

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

CodePudding user response:

Yes you could use a loop to work through all the paginated responses until you find the text you're looking for. To do that you'd have to know what to click on the page to move to the next page of results, and either a selector for something that indicates a page of results has finished loading or set a long enough wait time for each page to load

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

This is the simplest approach but will also be the slowest since you're going to need to set the wait value large enough to ensure the next set of results have been loaded each time. It would be more efficient to detect page changes that indicate the results are loading/loaded instead of just waiting up to some amount of seconds for the element to appear, however telling you how to do that would require a lot more information about the page and how it works (what elements appear/disappear, state changes, etc). There's also the chance this could create an endless loop unless you make sure the code you write to click the button for the next page of results realizes it's on the last page and doesn't just continue to keep trying to click for a new page of results.

  • Related