How to navigate to correct "URL" from outlook email content

Viewed 59

I'm successfully reading outlook email from JAVAX mail. But when i try to get the "Link" available in email body it's not giving the exact URL, instead it gives the URL with some extra characters like "=3D?*/". I tried to use below code but it didn't help me.

public List<String> getUrlsFromMessage(Message message, String linkText) throws Exception {
    String html = getMessageContent(message);
    List<String> allMatches = new ArrayList<String>();
    // (<a [^>]+>)
    Matcher matcher = Pattern.compile(" (<a [^>]+>)" + linkText + "</a>").matcher(html);
    while (matcher.find()) {
        String aTag = matcher.group(1);
        allMatches.add(aTag.substring(aTag.indexOf("http"), aTag.indexOf("\">")));
    }
    return allMatches;
}

Also I changed the pattern to

Pattern linkPattern = Pattern.compile(" <a\\b[^>]*href=\"([^\"]*)[^>]*>(.*?)</a>",
                        Pattern.CASE_INSENSITIVE | Pattern.DOTALL);`

But still it gives me the wrong URL.

1 Answers

Finally I found a solution to retrieve the exact URL using StringBuilder. What i did was i removed the unwanted characters from the string until i get the correct URL. This may not be a good coding practice but this was the only work around which works for me.

StringBuilder build = new StringBuilder(link);
        build.deleteCharAt(43);// Shift the positions front.
        build.deleteCharAt(51);
        build.deleteCharAt(51);
        driver.get(build.toString());
Related