我有以下HTML代码,我试图从中获取URL,但我不太确定如何检索它。我想要得到的href部分是www.boeing.com.au
这是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>
答案 0 :(得分:2)
你应该尝试如下: -
WebElement el = driver.findElement(By.linkText("Website"));
String urlText = el.getAttribute("href"); //this is the url
已修改: -
正如您提到的,您希望www.boeing.com.au
来自上面urlText
,那么您应该尝试以下方法: -
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)'
输出: - http://www.boeing.com.au/
希望它会帮助你...... :)