【问题标题】:java.net.MalformedURLException: unknown protocol: mapsjava.net.MalformedURLException:未知协议:地图
【发布时间】:2020-04-20 09:44:45
【问题描述】:

我在 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);
}

【问题讨论】:

  • 哪一行会报错?我怀疑这是一个铸造问题,与 Selenium 无关

标签: java selenium automation webdriver


【解决方案1】:

我建议您检查每个 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));
})

【讨论】:

    猜你喜欢
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2012-09-03
    • 2016-10-08
    • 1970-01-01
    相关资源
    最近更新 更多