首页 > 解决方案 > 如何使用 Selenium 和 Java 验证搜索结果中的所有单词以特定字母开头

问题描述

我在搜索字段中输入“A”并回车。我有一份员工名单。我需要验证搜索结果中所有以字母“A”开头的员工姓名。

标签: javaseleniumselenium-webdriver

解决方案


一个示例,您只需找到找到的所有员工姓名:

WebElement[] employees = driver.findElements(By. ...)
for (WebElement employee: employees) {
    if (!employee == null && employee.getText().substring(0, 1).toLowerCase().equals("a") {
        System.out.print(employee + " begins with 'a'")
    }
    else {
        System.out.print(employee + " does not begin with 'a'")
    }
}

我不知道源页面。也许可以通过以下方式(也)挖掘员工姓名:

employee.getAttribute("value")

推荐阅读