How can I make .goto non-blocking?

311 views Asked by At

I'm writing a rails app which fetches text from an HTML page using Watir and Chrome Headless. All good so far!

The problem starts when I request a page which has a long load time to completely load all elements despite the fact that I don't need them.

Current code I use:

browser = Watir::Browser.new :chrome, headless: true

browser.goto(url)

The .goto function call, however, blocks until ALL elements have loaded. That's not really what I need - what I need is for goto to just start fetching the page, then continue running code since I really just want to wait until the text I need is present, then fetch it.

Any ideas?

2

There are 2 answers

0
RAJ On BEST ANSWER

Goto will not leave the control until 60 seconds, If page load time exceeds 60 seconds, then it would throw the error. And also Watir.default_timeout has nothing to do with Goto's page loading. You need to set the timings for page_load which you can do by directly calling selenium driver as I have done below because Watir hasn't offered any systax for that

Write the below code, you could achieve what you want

 begin
  b.driver.manage.timeouts.page_load=5
  b=Watir::Browser.new
  b.goto(url)
rescue            #I have written the rescue block here because goto will the error for you If page is not loaded within a given time
end

AND THEN you can write your rest of the code here, for an example,

puts b.span(text: 'something').text

What happens here is, goto will be block the execution of the code followed by goto for 5 seconds, and then it would fall into the rescue block, so program would continue to execute next line as you expected.

1
titusfortner On

With the new w3c webdriver specification, you can set the page load strategy to 'none.' https://w3c.github.io/webdriver/webdriver-spec.html#navigation

Only Firefox and IE might have this implemented already.