d05
d05

Reputation: 1

Refreshing Webdriver page until element is displayed

I'm writing a script in Ruby with the Selenium-Webdriver and have been trying to come up with a loop that will refresh the current page until a certain element id is present. My current code looks like the following:

until driver.find_element(:id => "test").displayed?
 puts "#{driver.title} not live - reloading..."
 driver.navigate.refresh
end

However when executed, this brings the whole script to a halt without refreshing the page (but doesn't throw any errors). I tried substituting the driver.navigate.refresh with driver.get "http://website", but I get the same results.

In order to diagnose the problem, I've attempted to see what my first line is returning. When the element with id "test" is found, a puts driver.find_element(:id => "test").displayed? will return "true" to the console. If element "test" is not found, the console hangs without returning anything. It's my guess that I need to implement some kind of timer to force the script to give up searching for the element, but I was hoping it would figure this out dynamically (depending on how long it took to interpret the page).

Any help with getting this loop working would be greatly appreciated.

Upvotes: 0

Views: 1791

Answers (1)

TangibleDream
TangibleDream

Reputation: 611

I would replace

driver.navigate.refresh 

with

driver.navigate.to "http://page to refresh"

A little brute force, but it shouldn't stop the script this way.

I'd also add a wait command so you don't get into somekind of infinite loop.

wait = Selenium::WebDriver::Wait.new(:timeout => 30)
wait.until{driver.find_element(:name, 'test')}

If after 3 seconds there is still no sighting of the desired element, the page should refresh.

I hope this helps

Upvotes: 1

Related Questions