Selenium-webdriver and wait for the page to load

I am trying to write a simple test. My problem is that I want to wait until the page is fully loaded. At the moment, I am waiting for some elements to be presented, but this is not quite what I want. Is it possible to do something like this:

driver = Selenium::WebDriver.for :chrome driver.navigate.to url driver.wait_for_page_to_load "30000" 

Java is not a problem, but how to do it with ruby?

+8
ruby selenium webdriver
source share
5 answers

This is how Selenium docs () suggest:

 require 'rubygems' require 'selenium-webdriver' driver = Selenium::WebDriver.for :firefox driver.get "http://google.com" element = driver.find_element :name => "q" element.send_keys "Cheese!" element.submit puts "Page title is #{driver.title}" wait = Selenium::WebDriver::Wait.new(:timeout => 10) wait.until { driver.title.downcase.start_with? "cheese!" } puts "Page title is #{driver.title}" driver.quit 

If this is not an option, you can try the sentence from this SO post , although it will require some Javascript on top of Ruby / Rails.

It seems that wait.until is / is canceled. The new proposed process is to find the page in order to have that element that you know will be there:

 expect(page).to have_selector '#main_div_id' 
+13
source share

As far as I understand webdriver, you do not need to wait for the pages to load, because WebDriver has a blocking API , but you can set the page load timeout.

 driver.manage.timeouts.page_load = 10 # seconds 
+5
source share

This is no longer needed with WebDriver.

WebElement click () and Actions click () and "wait for page to load" if necessary automatically.

You can use imclicit and explicit (in that order) wait instead (described in seleniumhq) if you need to expect some ajax content, for example.

+2
source share

So, in Ruby, when you use get to open a URL, the ruby ​​script is executed ONLY when the page is fully loaded.

So, in your case, you just do: -

 driver.get url 
+2
source share

There have been times when changes to AJAX or CSS made my tests fail at times. I added these methods to my static driver instance so that, if necessary, I could check the test for certain conditions. (FROM#)

The TimedWait in the WaitForCssChange method is basically just Threading.Thread.Sleep This is not the most beautiful way, I think, but it works well for my needs.

To wait for Ajax:

  public static void WaitForAjax() { var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(25)); wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0")); } 

For CSS changes:

 public static void WaitForCssChange(IWebElement element, string value) { int counter = 0; while (true) { if(element.GetAttribute("style").Contains(value) || counter > 50) { break; } TimedWait(20); counter++; } } 
+1
source share

All Articles