我正在尝试从以下代码中获取电子邮件地址,但我不确定该怎么做。任何帮助将不胜感激
<div class="taLnk hvrIE6 fl" onclick="ta.trackEventOnPage('Listing', 'Email', 7741695, 1); return ta.call('ta.locationDetail.checkEmailAction',event,this,'info@email.com', 7741695, '\x41\x6e\x20\x69\x6e\x71\x75\x69\x72\x79\x20\x66\x72\x6f\x6d\x20\x61\x20\x54\x72\x69\x70\x41\x64\x76\x69\x73\x6f\x72\x20\x75\x73\x65\x72\x20\x66\x6f\x72\x20\x41\x6e\x6e\x61\x4c\x65\x6e\x61', 'Restaurant_Url_Restaurant_Review');">
最佳答案
您必须使用 Selenium 来获取 onclick 属性的值,然后使用 JAVA 字符串来分隔所需的电子邮件。
// find the element and get the attribute value of onclick
String onClickValue = driver.findElement(By.xpath("//div[@class= 'taLnk hvrIE6 fl']")).getAttribute("onclick");
// split the onclick value on ','
String[] allTextArray = onClickValue.split(",");
// iterate through the array and is @ is contained with the text, print text
for (String text : allTextArray) {
if (text.contains("@")) {
System.out.println(text);
return;
}
}
注意:div 的 XPath 可能不同。我已经根据您发布的小 HTML 编写了它。
关于javascript - Selenium 从 Div OnClick 元素中获取字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33608225/