【问题标题】:How to get HTML5 Cache status by using Selenium JavaScriptExecutor如何使用 Selenium JavaScriptExecutor 获取 HTML5 缓存状态
【发布时间】:2015-06-20 05:27:44
【问题描述】:

如何使用 Selenium JavaScriptExecutor 获取 HTML5 Cache 状态?

我尝试如下,但没有得到正确的状态。

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.html5.AppCacheStatus;
import org.openqa.selenium.html5.ApplicationCache;

public class Html5AppCache {

    public void testHTML5LocalStorage() throws Exception {

        WebDriver driver = new FirefoxDriver();

        driver.get("http://www.w3schools.com/html/tryhtml5_html_manifest.htm");

        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
        String cacheStatus = (String) jsExecutor.executeScript("return  window.applicationCache.status;");
    }

}

【问题讨论】:

    标签: java javascript html selenium


    【解决方案1】:

    在页面完全加载之前执行 javascript 时出现错误状态

    在执行javascript之前尝试等待页面中的元素被加载

    我还在上面的代码中发现了另一个问题,返回类型很长,不能转换为字符串,你会得到一个java.lang.ClassCastExceptionjava.lang.Long 不能转换为java.lang.String

    WebDriver driver = new FirefoxDriver();
    
    driver.get("http://www.w3schools.com/html/tryhtml5_html_manifest.htm");
    
    WebDriverWait wait = new WebDriverWait(driver,20);
    
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@id='timePara']")));
    
    JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
    
    long cacheStatus = (long) jsExecutor.executeScript("return  window.applicationCache.status;");
    
    System.out.println(cacheStatus);
    

    我尝试了上面的代码,我得到了正确的状态

    希望这对您有所帮助...如果有任何问题,请检查并回复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-09
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多