我们是否有任何通用函数来检查页面是否已在 Selenium 中完全加载 [英] Do we have any generic function to check if page has completely loaded in Selenium

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

问题描述

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

我尝试了以下代码:

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(网络驱动程序 ->((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

但即使页面正在加载,上面的代码也不会等待.

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

解决方案

正如你提到的,是否有任何通用函数可以通过 Selenium 答案是.

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

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

上述代码行中的参数 pageLoadTimeout 并没有真正与实际的 pageLoadTimeout().

您可以在此处找到有关 Selenium 中的 pageLoadTimeout 不工作的详细讨论<小时>

现在,由于您的用例与页面完全加载有关,您可以使用 pageLoadStrategy() 设置为 normal [ 支持的值为 noneeagernormal ] 使用通过 DesiredCapabilities 类或 ChromeOptions 类的实例作为如下:

  • 使用 DesiredCapabilities 类:

     import org.openqa.selenium.WebDriver;导入 org.openqa.selenium.firefox.FirefoxDriver;导入 org.openqa.selenium.firefox.FirefoxOptions;导入 org.openqa.selenium.remote.DesiredCapabilities;公开课 myDemo{public static void main(String[] args) 抛出异常{System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");DesiredCapabilities dcap = 新的 DesiredCapabilities();dcap.setCapability("pageLoadStrategy", "正常");FirefoxOptions opt = new FirefoxOptions();opt.merge(dcap);WebDriver 驱动程序 = 新的 FirefoxDriver(opt);driver.get("https://www.google.com/");System.out.println(driver.getTitle());驱动程序退出();}}

  • 使用 ChromeOptions 类:

    import org.openqa.selenium.WebDriver;导入 org.openqa.selenium.firefox.FirefoxDriver;导入 org.openqa.selenium.firefox.FirefoxOptions;导入 org.openqa.selenium.PageLoadStrategy;公开课 myDemo{public static void main(String[] args) 抛出异常{System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");FirefoxOptions opt = new FirefoxOptions();opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);WebDriver 驱动程序 = 新的 FirefoxDriver(opt);driver.get("https://www.google.com/");System.out.println(driver.getTitle());驱动程序退出();}}

您可以在 Chrome 驱动程序的页面加载策略(更新至 Selenium v​​3.12.0)

<小时>

现在将 PageLoadStrategy 设置为 NORMAL 并且您的代码试用都可以确保 浏览器客户端(即 Web 浏览器em>) 已经达到 'document.readyState' 等于 "complete".一旦满足这个条件,Selenium 就会执行下一行代码.

您可以在 Selenium IE WebDriver 中找到详细讨论调试时工作

但是 Browser Client 获得 'document.readyState' 等于 "complete" 仍然不能保证所有的 JavaScriptAjax 调用 已经完成.

<小时>

要等待所有 JavaScriptAjax 调用 完成,您可以编写如下函数:

public void WaitForAjax2Complete() 抛出 InterruptedException{而(真){if ((Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0")){休息;}线程睡眠(100);}}

详细讨论可以在等待ajax请求完成- 硒网络驱动程序

<小时>

现在,通过 PageLoadStrategy"return jQuery.active == 0" 的上述两种方法看起来是在等待不确定事件.因此,对于确定的等待,您可以引入 WebDriverWaitExpectedConditions 设置为 titleContains() 方法将确保页面标题(即网页)可见并假设所有元素也是可见的,如下所示:

driver.get("https://www.google.com/");new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("partial_title_of_application_under_test"));System.out.println(driver.getTitle());驱动程序退出();

<小时>

现在,有时虽然页面标题与您的应用程序标题匹配,但您想要交互的所需元素还没有完成加载.所以更细粒度的方法是引入 WebDriverWaitExpectedConditions 设置为 visibilityOfElementLocated() 方法将使您的程序等待所需的元素可见,如下所示:

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());驱动程序退出();

<小时>

参考文献

您可以在以下位置找到一些相关的详细讨论:

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

I tried below code:

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

解决方案

As you mentioned if there is any generic function to check if the page has completely loaded through 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"));

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


Now as your usecase relates to page being completely loaded you can use the pageLoadStrategy() set to normal [ the supported values being none, eager or normal ] using either through an instance of DesiredCapabilities Class or ChromeOptions Class as follows :

  • Using DesiredCapabilities Class :

     import org.openqa.selenium.WebDriver;
     import org.openqa.selenium.firefox.FirefoxDriver;
     import org.openqa.selenium.firefox.FirefoxOptions;
     import org.openqa.selenium.remote.DesiredCapabilities;
    
     public class myDemo 
     {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            DesiredCapabilities dcap = new DesiredCapabilities();
            dcap.setCapability("pageLoadStrategy", "normal");
            FirefoxOptions opt = new FirefoxOptions();
            opt.merge(dcap);
            WebDriver driver = new FirefoxDriver(opt);
            driver.get("https://www.google.com/");
            System.out.println(driver.getTitle());
            driver.quit();
        }
    }
    

  • Using ChromeOptions Class :

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.PageLoadStrategy;
    
    public class myDemo 
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            FirefoxOptions opt = new FirefoxOptions();
            opt.setPageLoadStrategy(PageLoadStrategy.NORMAL);
            WebDriver driver = new FirefoxDriver(opt);
            driver.get("https://www.google.com/");
            System.out.println(driver.getTitle());
            driver.quit();
        }
    }
    

You can find a detailed discussion in Page load strategy for Chrome driver (Updated till Selenium v3.12.0)


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.

You can find a detailed discussion in Selenium IE WebDriver only works while debugging

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


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

You can find a detailed discussion in Wait for ajax request to complete - selenium webdriver


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


References

You can find a couple of relevant detailed discussions in:

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

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