【问题标题】:How do I break a loop of elements in intern如何打破实习生中的元素循环
【发布时间】:2016-12-09 07:49:16
【问题描述】:

假设我有以下 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;
                    })
            }))
         })

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

感谢您提供的任何帮助

【问题讨论】:

  • 我不确定我是否理解您的问题。你能改写一下吗?是否有几个元素要单击或多个? Promise.all 将等到所有的承诺都得到解决,然后让它运行。如果您只想等到 one 解决,请查看Promise.race()
  • 感谢您的回复。我只想单击满足 if 语句的第一个元素,而不是它找到的第一个元素。 Promise.race 将不满足 if 语句,因为它将采用第一个元素

标签: javascript selenium selenium-webdriver intern


【解决方案1】:

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

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

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

返回所有tr 元素,其文本包含intern test 在id 为campaigns 的表格内。

使用索引,如0, 1, 2 来匹配包含intern testfirst, second and third 元素

【讨论】:

  • 感谢您的回复。但我的要求是单击 tr element ,而不是 td element ,您建议的 xpath 将无助于该要求
  • 编辑了我的答案。请试一试。刚刚添加“/..”来获取父级,即“tr”
  • 谢谢。这对我有帮助。需要在xpath上刷我的技能
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 2013-01-30
  • 2015-10-04
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多