rubycdp / ferrum

Headless Chrome Ruby API
https://ferrum.rubycdp.com
MIT License
1.71k stars 123 forks source link

Implement wait_for_selector #236

Open Mifrill opened 2 years ago

Mifrill commented 2 years ago

closes #82

dsisnero commented 2 years ago

Can this be merged? Do the linters need to be changed or do we want to follow the linter advice?

Mifrill commented 2 years ago

Hello @dsisnero this PR is on review, it can't be merged without approval. The linter offenses are not related to this particular PR, we have dedicated PR to solve it: https://github.com/rubycdp/ferrum/pull/237 that also on review.

route commented 2 years ago

I'm sorry guys, with all this stuff happening I barely find time for open source now, hope it's going to calm down soon.

Nakilon commented 2 years ago

If anyone needs a simple substitute of assert_selector or Selenium::WebDriver::Wait.new.until &block and google leads him here, here is a simple ruby snippet to wait for arbitrary things to return true:

timeout_true = lambda do |timeout = 1, &block|
  Timeout.timeout timeout do
    block.yield or (sleep 0.1; redo)
  end
end
shreeve commented 1 year ago

Any chance we could have a reviewer review and accept this? It would be very nice to have in the main Gem.

Thanks!

shreeve commented 1 year ago

Would this be simpler?

class Ferrum::Browser
  def wait_for(want, wait:1, step:0.1)
    meth = want.start_with?('/') ? :at_xpath : :at_css
    until node = send(meth, want)
      (wait -= step) > 0 ? sleep(step) : break
    end
    node
  end
end