如何使用量角器 select radiobuttonlist 中的值?

How to select value in radiobuttonlist with protractor?

我正在尝试检查单选按钮列表中的值:

<ul id="rbgHomeOccupier" class="radio-group cols-6">
            <li class="radio-group-button">
              <input type="radio" id="homeOccupierInjuredYes" class="radio-group-input" value="true"
                     tabindex="" name="homeOccupierInjured" ng-model="claim.damage.homeOccupierInjured" required>
              <label for="homeOccupierInjuredYes" class="radio-group-label">Yes</label>
            </li>
            <li class="radio-group-button">
              <input type="radio" id="homeOccupierInjuredNo" class="radio-group-input" value="false" tabindex=""
                     name="homeOccupierInjured"
                     ng-model="claim.damage.homeOccupierInjured" required>
              <label for="homeOccupierInjuredNo" class="radio-group-label">No</label>
            </li>
 </ul>

这是我试过的:

element.all(by.id('rbgHomeOccupier')).get(1).click();

但是我收到一条错误消息,提示只有一个元素:

 Failed: Index out of bound. Trying to access element at index: 1, but there are only 1 elements that match locator By.id("rbgHomeOccupier")

我怎样才能 select 第一个值,即 'Yes'?

您应该对 input 元素而不是列表元素执行 click() 操作。此外,元素定位器 by.id('rbgHomeOccupier') 不正确,因为在 DOM 和 element.all() tries to find all the elements in the DOM with that locator. Here's how to get the first radio input using get() 函数中只有一个元素具有该定位器,使用正确的定位器然后单击它 -

element.all(by.css('#rbgHomeOccupier input[type="radio"]')).get(0).click();
//Note: get() is a zero based index and so to click on the first element you need to pass 0 to the function

然而,如果您在 DOM 中只有一个 ID 为 homeOccupierInjuredYes 的输入元素,则有一种简单的方法。在这种情况下,您可以直接调用它而无需 get() 函数 -

element(by.id('homeOccupierInjuredYes')).click();

希望对您有所帮助。