Search code examples
javaoopselenium-webdriverautomationbuild-automation

Unable to delete all the records from table but its deleting only one record using selenium java


Trying to delete remove button after adding the products used for loop to delete the records one by one before signout the testcase

But its deleting only one record ,i would like to delete the records until its reach to table count zero

   ` List<WebElement> rows = driver.findElements(xpath);
        // For loop follows
        for (int i = 0; i < rows.size(); i++) {
            // Access individual elements this way:
            WebElement deleteButton = driver.findElement(xpath);
            deleteButton.click();
        }`

Solution

  • Probably you should search from root element from each row. It looks like you are trying to click on the same element in your loop each time.

    Try this

     List<WebElement> rows = driver.findElements(rowsXpath);
            // For loop follows
     for (WebElement element: rows) {
                // Access individual elements this way:
                WebElement deleteButton = element.findElement(deleteButtonXpath);
                deleteButton.click();
                // there I suggest to add some wait for rows amount to be decreased
    }`