我在Selenium Webdriver(Java)中遇到异常

java.net.MalformedURLException:未知协议:映射

检查页面上存在的所有活动链接(URL)时

代码如下:

for(int j=0;j<activeLinks.size();j++)
{
    String strURL = activeLinks.get(j).getAttribute("href");
    HttpURLConnection connection =  (HttpURLConnection)newURL(activeLinks.get(j).getAttribute("href")).openConnection();
    connection.connect();
    String response = connection.getResponseMessage(); //ok
    connection.disconnect();
    System.out.println(activeLinks.get(j).getAttribute("href")+"--> " +response);
}

最佳答案

我建议您检查每个href是否存在http / https协议,如果没有,则添加它。

例如:

activeLinks.forEach(link -> {
    String strURL = link.getAttribute("href");
    strUrl = strUrl.startsWith("http") ? strUrl : "https://".concat(strUrl);
    HttpURLConnection connection =  (HttpURLConnection) new URL(strUrl).openConnection();
    connection.connect();
    String response = connection.getResponseMessage(); //ok
    connection.disconnect();
    System.out.println(String.format("%s --> %S", strUrl,response));
})

关于java - java.net.MalformedURLException:未知协议(protocol):映射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59551270/

10-09 03:52