ruby selenium-webdriver 并等待页面加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11373351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selenium-webdriver and wait for page to load
提问by cupakob
I'm trying to write simple test. My problem is, that i want to wait until the page is loaded completly. At the moment i'm waiting until some elements are presen, but that is not really what i want. Is it possible to make something like this:
我正在尝试编写简单的测试。我的问题是,我想等到页面完全加载。目前我正在等待一些元素出现,但这并不是我真正想要的。是否有可能做这样的事情:
driver = Selenium::WebDriver.for :chrome
driver.navigate.to url
driver.wait_for_page_to_load "30000"
With Java isn't problem, but how to make it with ruby?
使用 Java 不是问题,但是如何使用 ruby 实现呢?
回答by ScottJShea
This is how the Selenium docs() suggest:
这就是Selenium docs() 的建议:
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 that is not an option you can try the suggestion from this SO postthough it would require some Javascript on top of the Ruby/Rails.
如果这不是一个选项,您可以尝试this SO post中的建议,尽管它需要在Ruby / Rails之上使用一些Javascript。
It seems that wait.untilis being/has been phased out. The new suggested process it to look for the page to have an element you know will be there:
似乎wait.until正在/已被淘汰。新建议的过程是寻找页面中包含您知道将在那里的元素:
expect(page).to have_selector '#main_div_id'
回答by vidit
As far as I understand webdriver, you dont need to wait for page loads because WebDriver has a blocking APIbut you can sure set a page load timeout.
据我了解 webdriver,您不需要等待页面加载,因为 WebDriver 具有阻塞 API,但您可以确定设置页面加载超时。
driver.manage.timeouts.page_load = 10 # seconds
回答by Amey
So in Ruby, whenever you use getto open a URL, the ruby script proceeds ONLY when the page completely loads.
因此,在 Ruby 中,每当您使用get打开 URL 时,ruby 脚本仅在页面完全加载时才会执行。
So in your case you would simply do :-
因此,在您的情况下,您只需这样做:-
driver.get url
回答by Franz Ebner
That's not needed with WebDriver anymore.
WebDriver 不再需要它了。
WebElement click() and Actions click() both "wait for page load" if needed automatically.
如果需要,WebElement click() 和 Actions click() 都会“等待页面加载”。
You can use imclicit and explicit (in this order) wait instead (described at seleniumhq) if you need to wait for some ajax content for instance.
例如,如果您需要等待某些 ajax 内容,您可以使用 imclicit 和显式(按此顺序)等待(在 seleniumhq 中描述)。
回答by VDubs
There have been instances where either AJAX or CSS changes caused my tests to fail at times. I added these methods to my static driver instance so that I can have the test wait for certain conditions if needed. (c#)
在某些情况下,AJAX 或 CSS 更改有时会导致我的测试失败。我将这些方法添加到我的静态驱动程序实例中,以便我可以在需要时让测试等待特定条件。(C#)
TimedWait in the WaitForCssChange Method is basically just a Threading.Thread.SleepThis is not the most beautiful way I guess, but it works well for my needs.
WaitForCssChange 方法中的 TimedWait 基本上只是一个Threading.Thread.Sleep我猜这不是最漂亮的方式,但它很适合我的需要。
For Ajax wait:
对于 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:
对于 CSS 更改:
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++;
}
}

