javascript - 如何打破实习生中的元素循环

标签 javascript selenium selenium-webdriver intern

假设我有以下 DOM 结构

<table id="campaigns">
   <tr>
      <th>Name</th>
      <th>Status</th>
   </tr>
   <tr>
      <td>first data</td>
   </tr>
   <tr data-id="1">
      <td>intern test at Fri, 09 Dec 2016 03:12:26 GMT</td>
   </tr>
   <tr data-id="2">
      <td>intern test at Fri, 09 Dec 2016 03:12:26 GMT</td>
   </tr>
   <tr data-id="3">
      <td>intern test at Fri, 09 Dec 2016 03:12:26 GMT edit</td>
   </tr>
   <tr data-id="4">
      <td>another data</td>
   </tr>
   <tr data-id="5">
      <td>next data</td>
   </tr>
</table>

这就是我在实习生中所做的

this.remote
     .findAllByXpath("//*[@id='campaigns']/tr"") //  get allitem from list
        .then(function (options) {
            return Promise.all(options.map(function (option) {
                return option.getVisibleText()
                    .then(function (text) {
                        if (text.includes("intern test")) {  //   checked whether the value is exist
                             option.click(); // click the value
                        }
                         return text;
                    })
            }))
         })

我的要求只是点击一个包含“实习生测试”的文本。 这段代码看起来不错,但发生的情况是,当实习生已经单击 if 语句中的第一个元素时,下一个元素似乎也会执行单击并陈旧该元素。 当找到第一个元素时,如何停止此循环并继续?

感谢您提供的任何帮助

最佳答案

无需遍历所有元素来查找包含文本“intern test”的第一个元素

使用 findAllByXpath 尝试以下 XPath(我不知道 Javascript - selenium,因此,如果需要,请更正语法):

//table[@id='campaigns']/tbody/tr/td[contains(text(),'intern test')]/..

返回 ID 为 campaigns 的表中文本包含 intern test 的所有 tr 元素。

使用索引,例如 0, 1, 2 来匹配包含 intern test第一个、第二个和第三个元素

关于javascript - 如何打破实习生中的元素循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41055505/

相关文章:

java - Selenium Web 驱动程序无法单击前台对话框的“确定”按钮并找到后台对话框的“确定”按钮

javascript - ECMAScript 6 或 7 是否支持静态类型?

javascript - a s 的设置值 :hidden in javascript

java - Selenium 2 : error: cannot parse capability: chromeOptions

java - 无法针对不同浏览器运行 testng.xml 文件中的回归组

javascript - Protractor 获取模型值

Javascript获取系统双击间隔?

javascript - 尝试停止在 react native 中加载 webView 时出现未定义错误

java - 如何将可迭代元素添加到列表中

python - 使用 Python、Selenium 和 XPath 抓取动态行表