Исключение с искаженным URL-адресом в Selenium WebDriver с использованием Java для поиска неработающих ссылок

Я использую этот код в сценарии Selenium для поиска неработающих ссылок. Итак, это код, который я написал, но при запуске я получаю искаженное исключение.

public void countNoOfLinksInHomePage(WebDriver fd) throws IOException{
        List<WebElement> listOfElements=fd.findElements(By.tagName("a"));
        //System.out.println(listOfElements.get(0));
        //log.info("name of links is " +listOfElements);
        int countOfElements=listOfElements.size();
        log.info("Total no of links in Homepage is:: " +countOfElements);

        //for(int i=0;i<countOfElements;i++){

            int responseCode=getResponseCode(listOfElements.get(1).getAttribute("href"));
            log.info("Response code of element at index 1 is:: " + responseCode);

            //break;
        //}
    }

    public static int getResponseCode(String url) throws MalformedURLException, IOException{

        URL u=new URL(url);
        HttpURLConnection huc=(HttpURLConnection)u.openConnection();
        huc.setRequestMethod("GET");
        huc.connect();
        return huc.getResponseCode();
    }

Трассировка testng:

java.net.MalformedURLException: неизвестный протокол: javascript


person Adam Grunt    schedule 26.01.2016    source источник
comment
Какая входная строка вызывает исключение?   -  person Andy Turner    schedule 26.01.2016
comment
javascript:(функция()%7Bvar%20doc=top.document;var%20bodyElement=document.body;doc.vtigerURL%20=%22http://localhost:8888/%22;var%20scriptElement=document.createElement(% 22script%22);scriptElement.type=%22text/javascript%22;scriptElement.src=doc.vtigerURL+%22modules/Emails/GmailBookmarkletTrigger.js%22;bodyElement.appendChild(scriptElement);%7D)();   -  person Adam Grunt    schedule 26.01.2016


Ответы (1)


Страница содержит якоря с javascript href:

<a href="javascript:..." 

Нет смысла тестировать эти ссылки, поэтому отфильтруйте их, как показано ниже:

String href = listOfElements.get(1).getAttribute("href");
if ((href != null) && !href.startsWith("javascript")) {
    int responseCode=getResponseCode(href);
    log.info("Response code of element at index 1 is:: " + responseCode);
}
person wero    schedule 26.01.2016