【发布时间】:2014-01-17 20:44:17
【问题描述】:
我有带有属性的 body 标记,我需要从中获取电子邮件地址以在 Selenium WebDriver 测试中断言。它可以通过默认定位器吗?
<body ng-init=" user={"id":43,"email":"EMAIL@gmail.com","username":"USER_NAME","balance":2259,"location":null,"d
escription":null,"fullname":"FULL_NAME","notification_settings":
{"newsletter":true,"follower":true,"mention":true,"comment":true},"settings":{"nsfw":false}};
user.small_avatar_url ='http://cdn.stage.uprise.com/user/43/small_34_34_20131111162316735';
" ng-class="(notice) ? 'with-notice' : ''" data-twttr-rendered="true" class="">
解决方案对我来说是什么工作在这里:
String nginit = this.getBrowser().findElement(By.tagName("body")).getAttribute("ng-init");
String[] arr = nginit.split(",");
for ( String ss : arr) {
if(ss.contains("email")) {
String[] email = ss.split("\"");
this.loggedinEmail = email[3];
}
}
【问题讨论】:
-
您使用什么语言绑定(例如 C#/Java)?你试过什么?
-
我想 ng-init 是包含大部分信息的主要属性。然后你可以使用 Driver.FindElement(By.ByTagName("Body")).getAttribute("ng-init");稍后您必须解析字符串以获取电子邮件以进行断言
-
我使用java。我通过运行 javascript getAttribute 方法发送电子邮件,然后遍历整个字符串。所以我也做了同样的事情,但使用的是 javascript 代码。
-
@brbrr:您是否尝试过与我在评论中提到的 Selenium 相同的方法?
-
@Anuragh27crony 是的,刚试过。效果很好。谢谢!
标签: html dom web selenium-webdriver