无法单击复选框输入元素但可以读取其值

问题描述 投票:0回答:3

在我的量角器测试声明中

await element(by.model('publishCtrl.isPublishedInAllRegions')) .click();

没有说“ElementNotVisibleError:元素不可见”

即使之前的断言,我添加到调试这个

expect(await element(by.model('publishCtrl.isPublishedInAllRegions')).isPresent())
    .toBeTruthy();

工作中。为什么会这样?此外,如果我不写这个断言,那么后面的读取复选框值的断言有效:

expect(await element(by.model('publishCtrl.isPublishedInAllRegions'))
        .getAttribute('value'))
 .toBeTruthy();

复选框定义及其父元素是

<label ng-transclude="" pan-form-option-type="checkbox" class="p6n-checkbox p6n-checkbox-label">
  <input type="checkbox" ng-model="publishCtrl.isPublishedInAllRegions" ng-click="publishCtrl.toggleAllRegionsSelection()" ng-required="!publishCtrl.publishedRegionsCount" class="ng-pristine ng-untouched ng-valid ng-not-empty ng-valid-required" jslog="47386;track:generic_click" aria-invalid="false">
  <span class="p6n-form-label" ng-transclude=""> All regions </span> 
</label>
javascript angularjs selenium selenium-webdriver protractor
3个回答
0
投票

单击radio / checkbox按钮输入元素的另一种方法是使用javascript executor。他不关心元素是否不可点击或其他状态。

或者,您通常可以单击包含在input元素内的label / span元素。

这是一个例子:

JS点击:

export async function jsClickButton(button: ElementFinder) {

    try {
        return await browser.executeScript('arguments[0].click()', button).then(async() => {
            console.log('Element has been clicked.');
        });
    } catch (error) {
        console.log('Element could not be clicked', error);
    }
}

或者正常点击(在您的情况下)。

await  element(by.xpath('//input[@ng-model=\'publishCtrl.isPublishedInAllRegions\']/following-sibling::span')).click();

0
投票

这里输入标签的高度可能为零,字面意思是不可见。但它存在于DOM中,您可以进行交互。

在这里,我们必须单击持有输入标签的标签,而不是单击输入标签。

你不能点击这个qazxsw poi的量角器js,其中inturn将使用css定位器访问元素为qazxsw poi。这将返回输入按钮而不是标签

要使用ng-model在angular中找到唯一的值,我们可以编写xpath之类的,

by.modal('modalvalue')

但我也看到ng-click仅配置为输入标签,模拟onclick。请尝试以上建议并添加您的评论。

如果不起作用,请通过使用javascript执行程序执行javascript直接单击锚点,因为它不可见。

[ng-model='publishCtrl.isPublishedInAllRegions']

或者简单地使用jquery

  await  element(by.xpath("//a[@ng-modal='publishCtrl.isPublishedInAllRegions']/parent::label")).click()

0
投票
var element =element(by.xpath("//a[@ng-modal='publishCtrl.isPublishedInAllRegions']");
browser.executeScript("arguments[0].click()",element);

并断言

browser.executeScript("$(\"input[ng-modal='publishCtrl.isPublishedInAllRegions'])\").click()",element);

希望以上答案对您有所帮助。尝试使用css查找元素。推荐用于量角器。

© www.soinside.com 2019 - 2024. All rights reserved.