如何在内部框架中执行带有循环的代码块

How can execute a block of code with loops into the intern framework

本文关键字:循环 代码 行带 执行 在内部 框架      更新时间:2023-09-26

我使用selenium的内部框架,我想执行一个查找表中元素的循环。循环查找每个元素并将其保存在数组中,获取元素后再进行操作。

思路是:

browser.wait(2000)            
.then(function () {
    while (true) {
    ifHasElements=browser.isDisplayed("/html/body/div[1]/div[5]/div[3]/div/div[3]/div[1]/table[2]/tbody/tr["+contRowsTable+"]").end()                    
    if (ifHasElements) {
        console.log("into if")                         
        browser.elementByXPath("/html/body/div[1]/div[5]/div[3]/div/div[3]/div[1]/table[2]/tbody/tr["+contRowsTable+"]/td[1]")
        .clickElement()
        .end()                    
        rows[contRowsTab]=browser.elementByXPath("/html/body/div[1]/div[5]/div[3]/div/div[3]/div[1]/table[2]/tbody/tr["+contRowsTable+"]")              
        } else {
           break
         }
    contRowsTab++;
    contRowsTable++;                  
    }
})

我不知道我是否执行了一个循环,同时在then块中获取元素。谁能帮我这个,非常感谢。

试试这样写:

var visibleRows = [];
var stopStoring = false;
return browser
    .wait(2000)
    .then(function () {
        return this.parent
            // find all the rows
            .findAllByXpath('/html/body/div[1]/div[5]/div[3]/div/div[3]/div[1]/table[2]/tbody/tr')
            .then(function (rows) {
                // store the visible rows until the first non-visible row is encountered
                return rows.reduce(function (chain, row) {
                    return chain.then(function () {
                        return row
                            .isVisible()
                            .then(function (isVisible) {
                                if (!isVisible) {
                                    stopStoring = true;
                                }
                                if (isVisible && !stopStoring) {
                                    visibleRows.push(row);
                                }
                            });
                    });
                }, this.parent);
            })
            .then(function () {
                // for each visible row, click the first td in the row
                return visibleRows.reduce(function (chain, row) {
                    return chain
                        .findByXpath('./td[1]')
                        .click()
                        .end();
                }, this.parent);
            });
    });

在这段代码中,我首先找到并存储可见行的第一个连续运行。然后,对于每一行,单击该行中的第一个表格单元格。