Java 你如何让 selenium 识别页面已加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/88269/
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
How do you get selenium to recognize that a page loaded?
提问by Matthew Jaskula
In certain unknown situations selenium does not detect that a page has loaded when using the open method. I am using the Java API. For example (This code will not produce this error. I don't know of an externally visible page that will.):
在某些未知情况下,selenium 在使用 open 方法时不会检测到页面已加载。我正在使用 Java API。例如(此代码不会产生此错误。我不知道会出现外部可见页面。):
Selenium browser = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
browser.start();
browser.open("http://www.google.com/webhp?hl=en");
browser.type("q", "hello world");
When the error occurs, the call to 'open' times out, even though you can clearly see that the page has loaded successfully before the timeout occurs. Increasing the timeout does not help. The call to 'type' never occurs, no progress is made.
发生错误时,对“打开”的调用超时,即使您可以清楚地看到页面在超时发生之前已成功加载。增加超时没有帮助。对“类型”的调用从未发生,也没有取得任何进展。
How do you get selenium to recognize that the page has loaded when this error occurs?
发生此错误时,如何让 selenium 识别页面已加载?
回答by Jim Deville
When I do Selenium testing, I wait to see if a certain element is visible (waitForVisible), then I do my action. I usually try to use an element after the one I'm typing in.
当我进行 Selenium 测试时,我会等待查看某个元素是否可见 (waitForVisible),然后我会执行我的操作。我通常尝试在我输入的元素之后使用一个元素。
回答by Matthew Jaskula
Enabling the 'multiWindow' feature solved the issue, though I am not clear why.
启用“multiWindow”功能解决了这个问题,但我不清楚原因。
SeleniumServer(int port, boolean slowResources, boolean multiWindow)
SeleniumServer(int port, boolean slowResources, boolean multiWindow)
SeleniumServer server = new SeleniumServer(4444, false, true);
Any clarification would be helpful.
任何澄清都会有所帮助。
回答by Peter Bernier
I've run into similar issues when using Selenium to test an application with iFrames. Basically, it seemed that once the primary page (the page containing the iframes) was loaded, Selenium was unable to determine when the iframe content had finished loading.
在使用 Selenium 测试带有 iFrame 的应用程序时,我遇到了类似的问题。基本上,一旦加载了主页面(包含 iframe 的页面),Selenium 就无法确定 iframe 内容何时完成加载。
From looking at the source for the link you're trying to load, it looks like there's some Javascript that's creating additional page elements once the page has loaded. I can't be sure, but it's possible that this is what's causing the problem since it seems similar to the situation that I've encountered above.
从查看您尝试加载的链接的源代码来看,似乎有一些 Javascript 会在页面加载后创建其他页面元素。我不能确定,但这可能是导致问题的原因,因为它似乎与我在上面遇到的情况相似。
Do you get the same sort of errors loading a static page? (ie, something with straight html)
您是否在加载静态页面时遇到相同类型的错误?(即,带有直接 html 的东西)
If you're unable to get a better answer, try the selenium forums, they're usually quite active and the Selenium devs do respond to good questions.
如果您无法获得更好的答案,请尝试 selenium 论坛,它们通常非常活跃,并且 Selenium 开发人员确实会回答很好的问题。
http://clearspace.openqa.org/community/selenium_remote_control
http://clearspace.openqa.org/community/selenium_remote_control
Also, if you haven't already tried it, add a call to browser.WaitForPageToLoad("15000") after the call to open. I've found that doing this after every page transition makes my tests a little more solid, even though it shouldn't technically be required. (When Selenium detects that the page actually has loaded, it continues, so the actual timeout variable isn't really a concern..
此外,如果您还没有尝试过,请在调用 open 之后添加对 browser.WaitForPageToLoad("15000") 的调用。我发现在每次页面转换后执行此操作会使我的测试更加可靠,即使从技术上讲它不应该是必需的。(当 Selenium 检测到页面实际上已经加载时,它会继续,所以实际的超时变量并不是真正的问题。.
回答by Lawrence
Using 'openAndWait' in place of 'open' will do the trick.
使用 'openAndWait' 代替 'open' 可以解决问题。
From the website:
从网站:
Many Actions can be called with the "AndWait" suffix, e.g. "clickAndWait". This suffix tells Selenium that the action will cause the browser to make a call to the server, and that Selenium should wait for a new page to load.
可以使用“AndWait”后缀调用许多操作,例如“clickAndWait”。这个后缀告诉 Selenium 该操作将导致浏览器调用服务器,并且 Selenium 应该等待新页面加载。
回答by Timur Evdokimov
I faced this problem quite recently.
我最近遇到了这个问题。
All JS-based solutions didn't quite fit ICEFaces 2.x + Selenium 2.x/Webdriver combination I have.
所有基于 JS 的解决方案都不太适合我拥有的 ICEFaces 2.x + Selenium 2.x/Webdriver 组合。
What I did and what worked for me is the following:
我所做的以及对我有用的内容如下:
In the corner of the screen, there's connection activity indicator.
在屏幕的一角,有连接活动指示器。
<ice:outputConnectionStatus id="connectStat"
showPopupOnDisconnect="true"/>
In my Java unit test, I wait until its 'idle' image comes back again:
在我的 Java 单元测试中,我一直等到它的“空闲”图像再次出现:
private void waitForAjax() throws InterruptedException {
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try {
if ("visibility: visible;".equals(
selenium.getAttribute("top_right_form:connectStat:connection-idle@style"))) {
break;
}
} catch (Exception e) {
}
Thread.sleep(1000);
}
}
You can disable rendering of this indicator in production build, if showing it at the page is unnecessary, or use empty 1x1 gifs as its images.
如果不需要在页面上显示该指标,您可以在生产版本中禁用该指标的呈现,或者使用空的 1x1 gif 作为其图像。
Works 100% (with popups, pushed messages etc.) and relieves you from the hell of specifying waitForElement(...) for each element separately.
100% 有效(使用弹出窗口、推送消息等),让您免于为每个元素分别指定 waitForElement(...) 的麻烦。
Hope this helps someone.
希望这可以帮助某人。
回答by dav
Not a perfect solution, but I am using this method
不是一个完美的解决方案,但我正在使用这种方法
$t1 = time(); // current timestamp
$this->selenium->waitForPageToLoad(30);
$t2 = time();
if ($t2 - $t1 >= 28) {
// page was not loaded
}
So, it is kind of checking if the page was not loaded during the specified time, so it is not loaded.
因此,它是一种检查页面是否在指定时间内未加载,因此未加载。
回答by someman
If you page has no AJAX, try to seek footer of page (I also use Junit fail("")
, you may use System.err.println()
instead):
如果您的页面没有 AJAX,请尝试查找页面的页脚(我也使用 Junit fail("")
,您可以System.err.println()
改用):
element.click();
int timeout =120;
// one loop = 0.5 sec, co it will be one minute
WebElement myFooter = null;
for(int i=0; i<timeout; i++){
myFooter = driver.findElement(By.id("footer"));
if(myFooter!= null){
break;
}
else{
timeout--;
}
}
if(timeout==0 && myFooter == null){
fail("ERROR! PAGE TIMEOUT");
}
回答by someman
another idea is to modify AJAX API (to add some text after AJAX actions). After ajax action was finished, before return, set invisible field to TRUE, selenium will find it and read as green-light
另一个想法是修改 AJAX API(在 AJAX 操作之后添加一些文本)。ajax动作完成后,返回前,将invisible field设置为TRUE,selenium会找到并读取为green-light
in html:
在 HTML 中:
<input type='hidden' id="greenlight">
in selenium
在硒
if(driver.findElement(By.id("greenlight")).getAttr("value").equals("TRUE")){
// do something after page loading
}
回答by Prabu Ananthakrishnan
Maybe this will help you....
也许这会帮助你......
Consider the following method is in page called Functions.java
考虑以下方法在名为 Functions.java 的页面中
public static void waitForPageLoaded(WebDriver driver) {
ExpectedCondition<Boolean> expectation = new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver,30);
try {
wait.until(expectation);
} catch(Throwable error) {
Assert.assertFalse(true, "Timeout waiting for Page Load Request to complete.");
}
}
And you can call this method into your function. Since it is a static method, you can directly call with the class name.
您可以将此方法调用到您的函数中。既然是静态方法,直接用类名调用即可。
public class Test(){
WebDriver driver;
@Test
public void testing(){
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
Functions.waitForPageLoaded(driver);
}
}