量角器-单击div中具有特定文本跨度的按钮

时间:2018-07-03 14:13:19

标签: javascript html css protractor automated-tests

我在单击量角器中的按钮时遇到问题。简化的代码如下所示:

<div class = "container">   
  <div class = "col1">
    <span class = "hour">12:00</span>   
  </div>

  <div class="col2">
    <button class="btn">Test</button>   
  </div>
</div>

<div class = "container">   
      <div class = "col1">
        <span class = "hour">13:00</span>   
      </div>

      <div class="col2">
      <button class="btn">Test</button>   
      </div>    
</div>

我想单击div中的按钮,该按钮的跨度为“ 12:00”小时。条件必须检查小时。

我可以轻松地用

检查范围
by.xpath('//span[contains(text(), "12:15")]')

我主要关心的是选择父div具有此范围的按钮。这可能吗? 谢谢。

2 个答案:

答案 0 :(得分:0)

我有类似的情况。但是我只需要从DOM中提取一个值,然后将其与预期结果进行比较,就象这样:

records = await targeting_page.records.getText();
        recordsNumber = parseFloat(records.replace(/,/g, ''));
        recordsRange = recordsNumber >= 100 && recordsNumber < 90000000;
        await expect(recordsRange).toEqual(true);

但是您的场合并没有太大的不同。

我在您问题页面上在浏览器控制台中写的内容

    async function fin() {
    for(var i = 0; i < 100; i++) {
    var myElement = document.querySelector("code span:nth-child(27)").textContent //your find your desired element
    console.log(b)
    typeof b == 'string' ? console.log(true) : console.log(false) //this is optionally 

    var res = parseFloat(b) //parse when it will be 12:00 you will get the 12 number
    console.log(res)
    res == 13 ? await b.click() : console.log("The button is not ready yet") //and here compare is there that number you need, if yes then click element(this doesn't work in console, 
// if you use async/await in your protractor tests, there are it will work
     }    
    }

    fin()

这就是直接与此页面有关的地方。 在测试中,它将看起来像这样(可以做的更好,但我非常方便地起草了):

for(let i=0; i < 100; i++) {
var myE = await myElement.getText(); //where myElement -- your selector
        var myNumber = parseFloat(myE.replace(/:/g, '')); // parse and get 12 out of "12:00"
        if (myNumber == 12) {
await myElement.click()
 } else {
  console.log("The button is not ready yet")
 }
}

答案 1 :(得分:0)

by.xpath('//span[contains(text(), "12:15")]/ancestor::div//button[@class='btn']') 希望这个答案对您有帮助!

0506