Selenium从Div OnClick元素获取字符串值 [英] Selenium get String value from Div OnClick element

查看:79
本文介绍了Selenium从Div OnClick元素获取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从以下代码中获取电子邮件地址,但不确定如何操作.任何帮助将不胜感激

Im trying to get the email address from the following code but im not sure how to do it. Any help would be appreciated

<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字符串来分离出所需的电子邮件.

You would have to use Selenium to get the value of the onclick attribute and then use JAVA Strings to seperate out the required email.

// 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内容进行了编写.

Note: The XPath of the div might be different. I have written it based on what little HTML you posted.

这篇关于Selenium从Div OnClick元素获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆