【问题标题】:How to print the specific element in the splitted values in selenium java如何在 selenium java 中打印拆分值中的特定元素
【发布时间】:2022-01-11 10:39:34
【问题描述】:
String ActualValue = element.getAttribute("class");
String[] SplittedValue =ActualValue.split("");

输出: object_selected object_notselected

如何在System.out.println(" ");中打印值object_selection

【问题讨论】:

  • The Java™ Tutorials - Arrays(参见创建、初始化和访问数组部分)
  • 请遵循 Java 命名约定 - 以后在调试代码时会感谢您自己。变量名以小写字母开头。
  • 不接受答案对你没有帮助

标签: java arrays selenium class split


【解决方案1】:

看,这个

SplittedValue

string array,可以多种方式打印数组:

String actualValue = driver.findElement(By.xpath("//")).getAttribute("class");
String[] splittedValue = actualValue.split("");
    
System.out.println("Length of array:" + splittedValue.length);
if (splittedValue.length > 0) {
    for(String str: splittedValue) {
            System.out.println(str);
    }
}
else {
    System.out.println("Since lenght is zero, split was not properly, you may wanna check what you are passing in split method");
    }

不知道你为什么使用这样的拆分split(""),可能你正在寻找这个split(" ")

另外,如果你确定字符串元素,那么你可以直接打印 他们喜欢这样(不推荐):

String actualValue = driver.findElement(By.xpath("//")).getAttribute("class");
String[] splittedValue = actualValue.split(" ");
    
System.out.println(splittedValue[0]);
System.out.println(splittedValue[1]);

【讨论】:

    【解决方案2】:

    你已经够近了。一旦您检索到 classname 的值,即 object_selected object_notselected,您可以调用 split(),以空格字符作为分隔符,如下所示:

    String ActualValue = element.getAttribute("class");
    // ActualValue = "object_selected object_notselected"
    
    String[] SplittedValue = ActualValue.split(" ");
    
    //printing object_selected
    System.out.println(SplittedValue[0]);
    
    //printing object_notselected
    System.out.println(SplittedValue[1]);
    

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      相关资源
      最近更新 更多