简体   繁体   中英

How to get the i value and perform click action?

.get(i) is not working

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.

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.

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.

In code, you probably want prdct.get(i).click() . And maybe after that addto.click() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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