user1798578 user1798578 - 5 months ago 22
Java Question

How to get part of the href inside an anchor tag

I have the following HTML code that I am trying to get the URL from but I am not too sure how to retrieve it. The part of the href I am trying to get is

www.boeing.com.au


Here's the HTML



<a class="ab_button" onmousedown="return rwt(this,'','','','16','AFQjCNFIVYkcHQ-z26ZbnFb9CgYWIbMU7Q','rSHXknRQFBYhkJW2qdZ-Pg','0ahUKEwiv-LLhyODNAhUEORoKHevZDV0Q_BcIhwEwDw','','',event)"
role="button" href="/url?sa=t&rct=j&q=&esrc=s&source=web&cd=16&cad=rja&uact=8&ved=0ahUKEwiv-LLhyODNAhUEORoKHevZDV0Q_BcIhwEwDw&url=http%3A%2F%2Fwww.boeing.com.au%2F&usg=AFQjCNFIVYkcHQ-z26ZbnFb9CgYWIbMU7Q&sig2=rSHXknRQFBYhkJW2qdZ-Pg"
>Website</a>




Answer

You should try as below :-

WebElement el = driver.findElement(By.linkText("Website"));
String urlText = el.getAttribute("href"); //this is the url

Edited :-

As you mentioned that you want www.boeing.com.au from the above urlText then you should try as below :-

public static Map<String, String> splitUrl(String urlText)  {
    Map<String, String> query_pairs = new LinkedHashMap<String, String>();
    String query = urlText;
    String[] pairs = query.split("&");
    for (String pair : pairs) {
        int idx = pair.indexOf("=");
        query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
    }
    return query_pairs;
}

Map<String, String> splittedUrlMap = splitUrl(urlText);
String actualUrl =  splittedUrlMap.get("url");
System.out.println(actualUrl)'

Output :- http://www.boeing.com.au/

Hope it will help you...:)