简体   繁体   English

如何获取 i 值并执行点击操作?

[英]How to get the i value and perform click action?

.get(i) is not working .get(i) 不工作

List<WebElement> prdct = driver.findElements(By.xpath("//h4[@class = 'product-name']"));
    WebElement addto = driver.findElement(By.xpath("//button[@test() = 'ADD TO CART']"));
    for (int i = 0; i < prdct.size(); i++) {

        String Name = prdct.get(i).getText();
        System.out.println(Name);
        if (Name.contains("Pomegranate")) 
        {
            addto.get
            break;
        }

    }

I need to get the i index and perform click action but get() is working, please check the enclosed image for better understanding.我需要获取 i 索引并执行单击操作,但 get() 正在运行,请查看随附的图像以更好地理解。

How to proceed with this?如何进行?

From your posted code it seems you should do the get(i) on prdct that is the list of elements if you have to perform an action with that element.从您发布的代码看来,如果您必须对该元素执行操作,您应该在prdct上执行 get(i) ,该操作是元素列表。

The addto seems to be an 'ADD TO CART' button and there is only one, you can perform the action in it as well but it's not a list so there is no possibility to use get in it. addto似乎是一个“添加到购物车”按钮,并且只有一个,您也可以在其中执行操作,但它不是列表,因此无法在其中使用 get。

In code, you probably want prdct.get(i).click() .在代码中,您可能需要prdct.get(i).click() And maybe after that addto.click() .也许在那之后addto.click()

addto不是List而是WebElement ,因此您不能使用get(i)WebElement接口不扩展List )。

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

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