【问题标题】:Get attribute from body tag. Selenium Webdriver从 body 标签中获取属性。 Selenium 网络驱动程序
【发布时间】: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


【解决方案1】:

由于电子邮件是“body”标签的“ng-init”属性的一部分值。我建议在 Body 标签的帮助下获取元素,然后检索“ng-init”元素。

稍后您可以解析字符串,以验证/使用电子邮件。

String Input= Driver.FindElement(By.ByTagName("Body")).getAttribute("ng-init");

然后用字符串分词器解析它,如下所示

StringTokenizer tokens=new StringTokenizer(Input,",");
String temp=null;
String Email=null;

    while(tokens.hasMoreTokens()){
            temp=tokens.nextToken();
        if(temp.contains("email")){
            Email=temp.split("\"")[3];
            System.out.println(Email);
            break;
        }

【讨论】:

    【解决方案2】:

    我不确定试试这个..

    option=driver.find_element_by_class_name("")
    email=option.get_attribute("email")
    

    【讨论】:

      猜你喜欢
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      相关资源
      最近更新 更多