【问题标题】:Selenium get String value from Div OnClick elementSelenium 从 Div OnClick 元素获取字符串值
【发布时间】:2016-02-10 01:05:55
【问题描述】:

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

<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');">

【问题讨论】:

  • 你尝试过什么?请分享该代码。

标签: javascript java html selenium


【解决方案1】:

您必须使用 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 编写的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-09
    • 2023-02-06
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2019-01-28
    相关资源
    最近更新 更多