本文介绍了我们是否有任何通用功能来检查页面是否已在Selenium中完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在selenium中检查网页是否已加载完成(即检查是否已加载所有控件)。

I am trying to check if web page is loaded completed or not (i.e. checking that all the control is loaded) in selenium.

我尝试下面的代码:

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
          webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

但即使页面加载上面代码也不会等待。

but even if page is loading above code does not wait.

我知道我可以检查特定元素以检查它是否可见/可点击等但我正在寻找一些通用解决方案

I know that I can check for particular element to check if its visible/clickable etc but I am looking for some generic solution

推荐答案

正如您所提到的,如果有任何通用功能来检查页面是否已在Selenium中完全加载,答案是

As you mentioned if there is any generic funtion to check if page has completely loaded in Selenium the answer is No.

首先让我们看看你的代码试用版如下:

First let us have a look at your code trial which is as follows :

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

上面一行代码中的参数 pageLoadTimeout 并不真正重新编码实际。

The parameter pageLoadTimeout in the above line of code doesn't really reseambles to actual pageLoadTimeout().

在这里,您可以找到有关的详细讨论

Here you can find a detailed discussion of pageLoadTimeout in Selenium not working

现在,当您的 usecase 与完全加载的页面相关时,您可以使用

现在将 PageLoadStrategy 设置为 正常 并且您的代码试用版都确保浏览器客户端具有(即 Web浏览器)已达到'document.readyState'等于完成。一旦满足此条件,Selenium将执行下一行代码。

Now setting PageLoadStrategy to NORMAL and your code trial both ensures that the Browser Client have (i.e. the Web Browser) have attained 'document.readyState' equal to "complete". Once this condition is fulfilled Selenium performs the next line of code.

您可以在

浏览器客户端获得'document.readyState'等于complete仍然不能保证所有 JavaScript Ajax Calls 都已完成。

But the Browser Client attaining 'document.readyState' equal to "complete" still doesn't guarantees that all the JavaScript and Ajax Calls have completed.

要等待所有 JavaScript Ajax Calls 完成,你可以写一个功能如下:

To wait for the all the JavaScript and Ajax Calls to complete you can write a function as follows :

public void WaitForAjax2Complete() throws InterruptedException
{
    while (true)
    {
        if ((Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0")){
            break;
        }
        Thread.sleep(100);
    }
}

您可以在

现在,上述两种方法通过 PageLoadStrategy 返回jQuery.active == 0看起来正在等待无限期事件。因此,对于明确的等待,你可以诱导与设置为方法将确保页面标题(即网页)可见并假设所有元素也可见如下:

Now, the above two approaches through PageLoadStrategy and "return jQuery.active == 0" looks to be waiting for indefinite events. So for a definite wait you can induce WebDriverWait inconjunction with ExpectedConditions set to titleContains() method which will ensure that the Page Title (i.e. the Web Page) is visible and assume the all the elements are also visible as follows :

driver.get("https://www.google.com/");
new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("partial_title_of_application_under_test"));
System.out.println(driver.getTitle());
driver.quit();






现在,有时可以通过页面标题将匹配您的应用程序标题仍然是您想要交互的所需元素尚未完成加载。所以更细粒度的方法是诱导与设置为方法,它将使您的程序等待所需的元素可见,如下所示:


Now, at times it is possible though the Page Title will match your Application Title still the desired element you want to interact haven't completed loading. So a more granular approach would be to induce WebDriverWait inconjunction with ExpectedConditions set to visibilityOfElementLocated() method which will make your program wait for the desired element to be visible as follows :

driver.get("https://www.google.com/");
WebElement ele = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_of_the_desired_element")));
System.out.println(ele.getText());
driver.quit();

这篇关于我们是否有任何通用功能来检查页面是否已在Selenium中完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:36