无法删除表中的所有记录,只能删除一个记录,使用Selenium Java。

huangapple go评论84阅读模式
英文:

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

问题

尝试在添加产品后删除移除按钮
使用循环逐个删除记录,然后在退出测试用例之前

但只删除一个记录,我想要删除记录直到表格计数为零

List<WebElement> rows = driver.findElements(xpath);
// 循环如下
for (int i = 0; i < rows.size(); i++) {
    // 通过以下方式访问单个元素:
    WebElement deleteButton = driver.findElement(xpath);
    deleteButton.click();
}
英文:

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&lt;WebElement&gt; rows = driver.findElements(xpath);
        // For loop follows
        for (int i = 0; i &lt; rows.size(); i++) {
            // Access individual elements this way:
            WebElement deleteButton = driver.findElement(xpath);
            deleteButton.click();
        }`

答案1

得分: 0

请参考以下翻译:

可能你应该从每行的根元素开始搜索。
看起来你在循环中每次都试图点击相同的元素。

试试这样做:

&lt;!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false --&gt;

&lt;!-- 语言: lang-js --&gt;

     List&lt;WebElement&gt; rows = driver.findElements(rowsXpath);
            // 循环如下
     for (WebElement element: rows) {
                // 这样访问单个元素:
                WebElement deleteButton = element.findElement(deleteButtonXpath);
                deleteButton.click();
                // 我建议在这里添加一些等待以确保行数减少
    }`

&lt;!-- 结束代码片段 --&gt;
英文:

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 List&lt;WebElement&gt; 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
}`

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月13日 13:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定