为什么此WebElement不返回完整的xpath?

时间:2016-10-27 23:50:48

标签: java selenium xpath selenium-webdriver webdriver

我有几个WebElements,执行以下

List<WebElement> customers = driver.findElements(By.xpath("//div[@id='Customers']/table/tbody/tr"));
System.out.println(customers.size());

会打印 5

那么为什么以下代码

List<WebElement> customers = driver.findElements(By.xpath("//div[@id='Customers']/table/tbody/tr"));

for (WebElement customer : customers) {
    if (customer.getText().equals("SQA")) {
        WebElement test = customer;
        System.out.println(test);
        break;
    }
}

打印xpath: //div[@id='Customers']/table/tbody/tr并且未能实际包含路径的特定索引?上面的xpath绝对没用;我期待找到 SQA 的位置。

xpath: //div[@id='Customers']/table/tbody/tr[4]

1 个答案:

答案 0 :(得分:2)

我认为它只打印用于查找元素的定位器。如果您想要索引,只需将代码更改为

即可
List<WebElement> customers = driver.findElements(By.xpath("//div[@id='Customers']/table/tbody/tr"));
for (int i = 0; i < customers.size(); i++)
{
    if (customers.get(i).getText().equals("SQA"))
    {
        System.out.println(i);
        break;
    }
}