【问题标题】:Verify link integrity to local html files验证本地 html 文件的链接完整性
【发布时间】:2018-11-04 00:19:25
【问题描述】:

有没有办法测试我的硬盘上的本地 html 文件的链接是否有效?我有几个 *.htm 文件,我在其中搜索文档中的所有链接。我正在搜索所有链接元素以及来自元素的 href 属性的链接。我尝试使用 silenium 和 HttpURLConnection 测试有效链接,但我想测试的 href 目标采用 file:///C://root//example.htm 的形式。这对我不起作用,因为它是一个本地文件并且我得到了 CastException:

URL url = new URL("file:///C://root//example.htm");

    HttpURLConnection httpURLConnect = (HttpURLConnection) url.openConnection();

    httpURLConnect.setConnectTimeout(1500);

    httpURLConnect.connect();

    if(httpURLConnect.getResponseCode() == 200)
    {
        System.out.println(href + " - " + httpURLConnect.getResponseMessage());
    }

【问题讨论】:

    标签: java html testing selenium-webdriver hyperlink


    【解决方案1】:

    您是否尝试过使用java.io.File 类?

    一个例子:

    public static void main(String[] args) {
        String filePath = "c:\\test1.txt";
        boolean fileExists = checkIfFileExists(filePath);
    
        System.out.println( String.format("The file in \"%s\" exists? %s", filePath, fileExists));
    }
    
    public static boolean checkIfFileExists(String filePath) {
        File file = new File(filePath);
        return file.isFile();
    }
    

    它将打印如下内容:

    “c:\test1.txt”中的文件存在吗?真的

    【讨论】:

    • 这是一个很好的观点,我也考虑过这一点,但我有时也有链接中的锚点,我也想检查目标链接中的锚点,例如file:///C://root//example.htm#myanchor
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多