gpt4 book ai didi

java - 如何获取 anchor 标记内的部分 href

转载 作者:行者123 更新时间:2023-12-02 03:29:54 33 4
gpt4 key购买 nike

我有以下 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>

最佳答案

您应该尝试如下:-

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

已编辑:-

正如您提到的,您希望从上面的urlText中获取www.boeing.com.au,那么您应该尝试如下:-

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/

希望对你有帮助...:)

关于java - 如何获取 anchor 标记内的部分 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38237853/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com