javascript - Selenium 从 Div OnClick 元素中获取字符串值

标签 javascript java html selenium

我正在尝试从以下代码中获取电子邮件地址,但我不确定该怎么做。任何帮助将不胜感激

<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/

相关文章:

c# - 正确调整包含未知尺寸图像的 iframe 的大小

java - 时间段的数学集?

javascript - 如何减小 html5 音频缓冲区的大小?

javascript - 在两个 div 之间切换时更新页面的高度

javascript - 为不同的 WordPress 页面添加自定义样式表

javascript - Sencha Touch 将基本身份验证注入(inject)所有请求

java - 通过 Mongoose (Node JS) 在 Mongodb 中插入没有 JSON 模式的 JSON 对象

javascript - 创建一个水平滚动的 Div

javascript - 我需要更改 cookie 的 javascript 代码。我正在保存输入文本,但我需要保存 div 文本而不是输入文本

java - 生成swagger java客户端代码时出错