你怎么得到selenium来识别一个页面加载?

在某些未知情况下,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"); 

发生错误时,即使您可以在超时发生之前清楚地看到页面已成功加载,对“打开”的调用也会超时。 增加超时没有帮助。 对’type’的调用永远不会发生,没有进展。

当发生此错误时,如何让selenium识别页面已加载?

我最近遇到了这个问题。

所有基于JS的解决方案都不太适合ICEFaces 2.x + Selenium 2.x / Webdriver组合。

我做了什么以及对我有用的是以下内容:

在屏幕的角落,有连接活动指示器。

   

在我的Javaunit testing中,我等到它的“空闲”图像再次出现:

 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); } } 

您可以在生产版本中禁用此指标的呈现,如果在页面上显示它是不必要的,或使用空的1×1 GIF作为其图像。

100%工作(使用弹出窗口,推送消息等)并使您免于分别为每个元素指定waitForElement(…)。

希望这有助于某人。

也许这会对你有所帮助….

请考虑以下方法在名为Functions.java的页面中

 public static void waitForPageLoaded(WebDriver driver) { ExpectedCondition expectation = new ExpectedCondition() { 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."); } } 

您可以将此方法调用到您的函数中。 由于它是静态方法,因此可以直接使用类名调用。

 public class Test(){ WebDriver driver; @Test public void testing(){ driver = new FirefoxDriver(); driver.get("http://www.gmail.com"); Functions.waitForPageLoaded(driver); } } 

当我进行Selenium测试时,我等待查看某个元素是否可见(waitForVisible),然后我做我的行动。 我经常尝试在我输入的元素之后使用一个元素。

用’openAndWait’代替’open’就可以了。

来自网站 :

可以使用“AndWait”后缀调用许多Actions,例如“clickAndWait”。 此后缀告诉Selenium该操作将导致浏览器调用服务器,并且Selenium应该等待加载新页面。

启用’multiWindow’function解决了这个问题,但我不明白为什么。

SeleniumServer(int port,boolean slowResources,boolean multiWindow)

 SeleniumServer server = new SeleniumServer(4444, false, true); 

任何澄清都会有所帮助。

使用Selenium测试iFrames的应用程序时,我遇到了类似的问题。 基本上,似乎一旦主页面(包含iframe的页面)被加载,Selenium就无法确定iframe内容何时完成加载。

从查看您尝试加载的链接的源代码,看起来有一些Javascript在页面加载后创建了其他页面元素。 我不能确定,但​​这可能是导致问题的原因,因为它看起来与我上面遇到的情况类似。

加载静态页面时会出现同样的错误吗? (即直接html的东西)

如果您无法获得更好的答案,请尝试使用selenium论坛,他们通常非常活跃,Selenium开发人员会回答好问题。

http://clearspace.openqa.org/community/selenium_remote_control

此外,如果您还没有尝试过,请在打开调用后添加对browser.WaitForPageToLoad(“15000”)的调用。 我发现在每次页面转换后执行此操作会使我的测试更加稳固,即使技术上不需要它。 (当Selenium检测到页面实际已加载时,它会继续,因此实际的超时变量并不是真正的问题。

不是一个完美的解决方案,但我正在使用这种方法

 $t1 = time(); // current timestamp $this->selenium->waitForPageToLoad(30); $t2 = time(); if ($t2 - $t1 >= 28) { // page was not loaded } 

因此,它检查页面是否在指定时间内未加载,因此未加载。

另一个想法是修改AJAX API(在AJAX动作之后添加一些文本)。 ajax动作完成后,在返回之前,将不可见字段设置为TRUE,selenium将找到它并读作绿灯

在HTML中:

  

在selenium中

 if(driver.findElement(By.id("greenlight")).getAttr("value").equals("TRUE")){ // do something after page loading } 

如果您的页面没有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