Selenium Webdriver执行由于javascript等待条件而被卡住 [英] Selenium webdriver execution gets stuck due to a javascript wait condition

查看:803
本文介绍了Selenium Webdriver执行由于javascript等待条件而被卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Java脚本等待条件来等待页面加载,而我的代码如下:

I have been using a java-script wait condition to wait for page load and my code is like this:

public void untilPageIsLoaded() {
        ExpectedCondition<Boolean> condition = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
            }
        };
        WebDriverWait wait = new WebDriverWait(driver, timeout);
        wait.until(condition);
    }

最初,此代码运行良好,但几天后,我的测试开始陷入随机点.因此,我调查了这个问题,发现罪魁祸首是在Java-script等待条件之上,该条件会停止执行.

Initially this code was working very well but few days later my tests started getting stuck at random points. So I investigated the issue and came to know that the culprit is above java-script wait condition which stops execution.

即使我没有遇到任何异常,它也永远不会超时.我知道这很奇怪,但这不仅在我身上,在这里看看.

Even I was not getting any exception and it never timeout.I know this is very strange but this is not only with me, have a look here;

https://code.google.com/p/selenium/issues/detail?id = 6955

我尝试了硒和浏览器的升级/降级版本,更改了系统配置,但没有一个起作用.

I tried with Upgraded/downgraded version of selenium and browser,changed system configuration but none of them worked.

现在我想用其他代码替换上面的java-script等待条件.我不想使用thread.sleep();

Now I want to replace above java-script wait condition with some other code.I don't want to use thread.sleep();

请给我推荐一些好东西.

Please suggest me something good.

推荐答案

在文档准备就绪之前,此代码将永远不会执行.

This code would never execute until the document is already ready anyway.

因此,无需从硒中执行此JS-因为它只能返回一个结果-完成".

So there is no need to execute this JS from selenium - because it can only ever return one result - "complete."

这是因为只有在页面上才能从SE执行JS.进入页面的唯一方法是调用Selenium的.get(),它会阻止所有后续执行,直到DOM完全加载为止.所以.get()已经在做您的.untilPageIsLoaded()否则会做的事情.

This is because you can't execute JS from SE until you are on a page. The only way to get on a page is to call Selenium's .get(), which blocks any following execution until the DOM is fully loaded. So .get() is already doing what your .untilPageIsLoaded() would otherwise do.

这篇关于Selenium Webdriver执行由于javascript等待条件而被卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆