view raw
user1798578 user1798578 - 10 months ago 43
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

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&"


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

Output :-

Hope it will help you...:)