如何通过while循环验证WebElement是否存在

时间:2018-08-21 16:15:30

标签: java selenium while-loop

我添加了它以创建一个随机的组名,现在我要验证此组名是否存在:

public String generateRandomName(int length) {
    char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
            .toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < length; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }
    String randomString = sb.toString();
    return randomString;
}

这是我的while循环:

public boolean validateGroupNameIsCorrect(String groupName) {
    int i = 1;
    WebElement NewGroupText = driver.findElement(By.xpath("//*[@id='content']/form/span["+ i + "]"));
    while (true) {
        if (getNewGroupText().getText().equals(groupName));
        return true;
        System.out.println(i);
    }
}

1 个答案:

答案 0 :(得分:0)

使用xpath查找元素,不需要循环:

public boolean validateGroupNameIsCorrect(String groupName) {
    return driver.findElements(By.xpath("//*[@id='content']/form/span[.='"+ groupName + "']")).size()>0;
}