当前位置: 首页 > 知识库问答 >
问题:

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

公冶桐
2023-03-14
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
          webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

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

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

共有1个答案

贺飞
2023-03-14

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

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

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

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

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

但是将'document.readystate'等于“complete”的浏览器客户端仍然不能保证所有的JavaScript和Ajax调用都已经完成。

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

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

您可以在等待ajax请求完成-selenium webdriver中找到详细的讨论

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();
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();
    null
 类似资料:
  • 问题内容: 想象一下,您单击页面上使用的元素,并想从结果页面中检索结果。如何检查以确保生成的页面已加载?我可以在处理页面和单击元素之间插入,但这似乎是一种非常丑陋且缓慢的方法。 问题答案: 设置然后在页面上搜索元素。从 setImplicitWaitTimeout(毫秒= 10000) 设置驱动程序在搜索元素时应等待的时间。当搜索单个元素时,驱动程序将轮询页面,直到找到一个元素或超时到期为止,以先

  • 所以我想知道,我想看看一个表是否已经被懒/急加载了,而没有在我检查时实际加载它。例如: 所以当我设置($this)时-

  • 我目前正在建立一个网站,通过JavaScript使用自定义快速导航。 如果用户按右键,他将被导航到“我的页面列表”中的下一页,通过按ALT Right或Left键,他可以在浏览器历史记录中前后导航。 这是我当前的代码: 这一切都很好,除了一个小问题:假设我想返回三页-如果我是一个有逻辑思维的人,我会先按ALT Left返回一个站点,然后当我在上一个站点上时,只需按住ALT,然后再次按Left返回一

  • 我有一个服务器端应用程序,客户端可以请求重新加载配置。如果一个客户端请求重新加载配置,这不应该立即完成,而是延迟1分钟。如果另一个客户端也在同一分钟内请求重新加载配置,这个请求应该被忽略。 我的想法是安排一个任务与调度ExecutorService像: 如何检查LoadConfigurationTask是否已计划但尚未执行,以便在重新加载配置之前忽略进一步的请求?

  • 问题内容: 检测下载是否完成的最佳方法是什么,因为此后我想更新数据库。 我尝试了PHP手册中的一些代码,但对我来说并没有多大作用: 问题答案: 但这并不能真正告诉您用户是否已完成下载。当 您 完成向用户发送字节后,它将告诉您,但它并没有说明用户实际收到了多少字节。 实际上,PHP(服务器端而非客户端语言)无法真正检测文件下载何时完成。最好的办法是在下载开始时将下载记录在数据库中。如果您绝对,完全和