简体   繁体   English

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

[英]Protractor - Clicking button in div which has a span with certain text

I have a problem with clicking a button in Protractor. 单击量角器中的按钮时出现问题。 Simplified code looks like that: 简化的代码如下所示:

<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>

I would like to click a button in div, where its span has "12:00" hour. 我想单击div中跨度为“ 12:00”的按钮。 The condition must check the hour. 条件必须检查小时。

I can easily check the span with 我可以轻松地检查跨度

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

My main concern is to select the button with parent div having this span. 我主要关心的是选择父div具有此范围的按钮。 Is this possible? 这可能吗? Thank you. 谢谢。

I had a similar condition. 我有类似的情况。 But I had just to extract a value from DOM and compare it with the expected result, it looks like this: 但是我只需要从DOM中提取一个值,然后将其与预期结果进行比较,结果如下所示:

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

but your occasion is not a big difference. 但是你的场合差别不大。

that what I have written in browser console on this page of your question 我在您问题页面上在浏览器控制台中写的内容

    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()

That's what concerns directly this page. 这就是直接与此页面有关的内容。 In test, it will be looking something like this(it can be much better but I drafted it very quckly): 在测试中,它将看起来像这样(可以做得更好,但我非常乐意起草):

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

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

0506 0506

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

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