我们是否有任何通用函数来检查页面是否已完全加载到Selenium中

2024-06-28 14:40:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我试过以下代码:

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

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

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


Tags: 代码网页newreturnseleniumdocument控件until
3条回答

实施这个,它对我们很多人都有效,包括我。它包括JavaScript、Angular、JQuery等网页。在

如果您的应用程序包含Javascript&JQuery,那么您只能为它们编写代码

通过在单个方法中定义它,您可以在任何地方调用它:

          // Wait for jQuery to load
          {             
            ExpectedCondition<Boolean> jQueryLoad = driver -> ((Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active") == 0);

            boolean jqueryReady = (Boolean) js.executeScript("return jQuery.active==0");

            if (!jqueryReady) {
                // System.out.println("JQuery is NOT Ready!");
                wait.until(jQueryLoad);
            }
            wait.until(jQueryLoad);
          }

          // Wait for ANGULAR to load
          {               
            String angularReadyScript = "return angular.element(document).injector().get('$http').pendingRequests.length === 0";

            ExpectedCondition<Boolean> angularLoad = driver -> Boolean.valueOf(((JavascriptExecutor) driver).executeScript(angularReadyScript).toString());

            boolean angularReady = Boolean.valueOf(js.executeScript(angularReadyScript).toString());

            if (!angularReady) {
                // System.out.println("ANGULAR is NOT Ready!");
                wait.until(angularLoad);
            }
          }

          // Wait for Javascript to load    
          {             
            ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString()
                    .equals("complete");

            boolean jsReady = (Boolean) js.executeScript("return document.readyState").toString().equals("complete");

            // Wait Javascript until it is Ready!
            if (!jsReady) {
                // System.out.println("JS in NOT Ready!");
                wait.until(jsLoad);
            }
          }

Click here for Reference Link

如果你在执行中遇到任何问题,请告诉我。在

它克服了线程或显式等待的使用。在

我也使用selenium,我也遇到了同样的问题,为了解决这个问题,我只需要等待jQuery加载。在

所以如果你有同样的问题,也试试这个

((Long) ((JavascriptExecutor) browser).executeScript("return jQuery.active") == 0);

您可以将这两个函数包装在一个方法中并进行检查,直到页面和jQuery都被加载

正如您提到的,如果有任何泛型函数来检查页面是否已通过Selenium完全加载,答案是。在

首先让我们看看您的代码试用版,如下所示:

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

上面代码行中的参数pageLoadTimeout并没有真正地重新编译为实际的pageLoadTimeout()。在

在这里您可以找到关于pageLoadTimeout in Selenium not working的详细讨论


现在,当您的用例与页面完全加载相关时,您可以使用设置为normal[支持的值是noneeagernormal]的实例,如下所示:

  • 使用DesiredCapabilities类:

     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();
        }
    }
    
  • 使用色度选项类:

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

您可以在Page load strategy for Chrome driver (Updated till Selenium v3.12.0)中找到详细的讨论


现在,将PageLoadStrategy设置为NORMAL和您的代码试用都可以确保浏览器客户端(即Web浏览器)的'document.readyState'等于"complete"。一旦满足了这个条件,Selenium将执行下一行代码。在

您可以在Selenium IE WebDriver only works while debugging中找到详细的讨论

但是浏览器客户端获得'document.readyState'等于"complete"仍然不能保证所有的JavaScriptAjax调用都已完成。在


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

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

您可以在Wait for ajax request to complete - selenium webdriver中找到详细的讨论


现在,通过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());
driver.quit();

现在,虽然页面标题与应用程序标题匹配,但是您想要交互的元素还没有完成加载。因此,更细粒度的方法是将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());
driver.quit();

相关问题 更多 >