简体   繁体   English

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

[英]Why is this WebElement not returning the full xpath?

I have several WebElements such that executing the following 我有几个WebElements可以执行以下命令

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

would print 5 . 将打印5

So then why does the following code 那为什么下面的代码

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;
    }
}

print xpath: //div[@id='Customers']/table/tbody/tr and fail to actually include the specific index of the path ? 打印xpath: //div[@id='Customers']/table/tbody/tr并没有实际包含路径特定索引 The above xpath is absolutely useless; 上面的xpath绝对没有用; I'm expecting the location of where SQA was found. 我期望找到SQA的位置。

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

I think it just prints the locator used to find the element. 我认为它只是打印用于查找元素的定位器。 If you want the index, just change your code to 如果需要索引,只需将代码更改为

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM