首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Selenium WebDriver:等待加载包含JavaScript的复杂页面

Selenium WebDriver:等待加载包含JavaScript的复杂页面
EN

Stack Overflow用户
提问于 2012-05-23 20:52:34
回答 18查看 192.7K关注 0票数 111

我有一个使用Selenium进行测试的web应用程序。在页面加载过程中有很多JavaScript在运行。

这段JavaScript代码写得不是很好,但是我不能修改任何东西。所以等待一个元素出现在DOM中

方法不是一个选项。

我想在Java中创建一个泛型函数来等待页面加载,一个可能的解决方案是:

从WebDriver运行JavaScript脚本并存储以下结果

在字符串变量中

..。

比较

变量设置为以前版本的

..。如果它们相同,则设置递增计数器

其他设置

归零。

稍等片刻(例如50毫秒)。

如果页面已经有一段时间没有更改(例如500ms),那么

然后退出循环,否则循环到第一步。

你认为这是一个有效的解决方案吗?

EN

回答 18

Stack Overflow用户

回答已采纳

发布于 2012-05-24 03:12:23

如果有人真的知道一个通用且始终适用的答案,那么它应该已经实现了。

无处不在

会让我们的生活变得更轻松。

你可以做很多事情,但每一件事情都有一个问题:

正如Ashwin Prabhu所说,如果你很了解脚本,你可以观察它的行为并跟踪它的一些变量

或者

然而,这个解决方案并不适用于所有人,只能由您使用,并且只能在有限的页面上使用。

通过观察HTML代码以及它是否经过一段时间的更改,您的解决方案是不错的(还有

一种方法

直接获取原始且未编辑的HTML

),但是:

代码语言:javascript
复制
- It takes a long time to actually assert a page and could prolong the test significantly.
- You never know what the right interval is. The script _might_ be downloading something big that takes more than 500 ms. There are several scripts on our company's internal page that take several seconds in IE. Your computer may be temporarily short on resources - say that an antivirus will make your CPU work fully, then 500 ms may be too short even for a noncomplex scripts.
- Some scripts are never done. They call themselves with some delay (`setTimeout()`) and work again and again and could possibly change the HTML every time they run. Seriously, every "Web 2.0" page does it. Even Stack Overflow. You could overwrite the most common methods used and consider the scripts that use them as completed, but ... you can't be sure.
- What if the script does something other than changing the HTML? It could do thousands of things, not just some `innerHTML` fun.

在这方面,有一些工具可以帮助你。即

进度监听器

nsIWebProgressListener

还有其他一些。然而,浏览器对此的支持非常糟糕。火狐开始尝试从FF4开始支持它(仍在发展中),IE在IE9中有基本的支持。

我想我很快就会想出另一个有缺陷的解决方案。事实是-没有确切的答案来回答什么时候说“现在页面是完整的”,因为永恒的脚本在做他们的工作。选择最适合你的,但要注意它的缺点。

票数 77
EN

Stack Overflow用户

发布于 2013-10-18 01:25:02

谢谢阿什温!

在我的例子中,我需要等待某个元素中的jquery插件执行。特别是"qtip“

根据你的提示,它对我来说非常有效:

代码语言:javascript
复制
wait.until( new Predicate() {
            public boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        }
    );

注意:我使用的是Webdriver 2

票数 32
EN

Stack Overflow用户

发布于 2015-05-06 00:59:14

您需要等待Javascript和jQuery完成加载。执行Javascript检查是否

,这意味着JS和jQuery加载完成。

代码语言:javascript
复制
public boolean waitForJStoLoad() {

    WebDriverWait wait = new WebDriverWait(driver, 30);

    // wait for jQuery to load
    ExpectedCondition jQueryLoad = new ExpectedCondition() {
      @Override
      public Boolean apply(WebDriver driver) {
        try {
          return ((Long)executeJavaScript("return jQuery.active") == 0);
        }
        catch (Exception e) {
          return true;
        }
      }
    };

    // wait for Javascript to load
    ExpectedCondition jsLoad = new ExpectedCondition() {
      @Override
      public Boolean apply(WebDriver driver) {
        return executeJavaScript("return document.readyState")
            .toString().equals("complete");
      }
    };

  return wait.until(jQueryLoad) && wait.until(jsLoad);
}
票数 27
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10720325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档