简体   繁体   中英

java.net.MalformedURLException: unknown protocol: maps

I am getting Exception in Selenium Webdriver (Java)

java.net.MalformedURLException: unknown protocol: maps

While Checking all Active Links(URLs) present on the page

The Code is as follows:

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

I suggest you check if http/https protocol is present for each href and add it if not.

ex:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM