简体   繁体   English

如何在实习生中打破元素循环

[英]How do I break a loop of elements in intern

Let's say I have the following DOM structure 假设我有以下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>

and this is how i do it in intern 这就是我在实习生中的表现

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;
                    })
            }))
         })

my requirement is only to click one the text that contain " intern test ". 我的要求是只点击一个包含“ 实习生测试 ”的文本。 This code seems fine but what happens is that when intern already clicking the first element in the if statement, the next element seems will also perform the click and stale the element. 这段代码似乎很好,但是当实习生已经单击if语句中的第一个元素时,下一个元素似乎也将执行单击并使元素失效。 How do I stop this loop and proceed when it found the 1st element? 如何在发现第一个元素时停止此循环并继续?

Thanks for any help provide 感谢您提供的任何帮助

There is no need to go through all the elements to find the first element containing text "intern test" 没有必要通过所有元素来找到包含文本“实习生测试”的第一个元素

Try the following XPath using findAllByXpath (I am not aware of Javascript - selenium, so, please correct the syntax if required): 使用findAllByXpath尝试以下XPath(我不知道Javascript - selenium,所以,如果需要,请更正语法):

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

returns all the tr elements with text containing intern test inside the table with id campaigns . 返回包含带有内部intern test文本的所有tr元素,其中包含id campaigns

Use indexing, like 0, 1, 2 to match first, second and third elements containing intern test 使用索引(如0, 1, 2来匹配包含intern test first, second and third元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM