【问题标题】:can't click on element that has multiple options无法单击具有多个选项的元素
【发布时间】:2017-07-18 15:28:43
【问题描述】:

我正在尝试通过以下方式单击一个元素,但收到错误消息:

Failed: Cannot read property 'click' of undefined

代码:

'use strict;'
var PresentPage = function(){
    let PresentPageTitle = element(by.xpath("//*[@id='planAndDev']/div/div/div/div/matanot/form/div[2]/h4"));
    let UserPhone = element(by.id("tel"));
    let Email = element(by.id("email"));
    var AresForPresents = element(by.xpath("//*[@id='verticalRadioGrp']/div/select")).all(by.tagName("option")).then(function(options)
    { 
      return options;
    });
     this.SelectAreaToGetPresent = function()
    { 
        AresForPresents[3].click(); 
    };
};
module.exports = new PresentPage();

编辑: 在函数中使用它时可以正常工作。

this.SelectAreaToGetPresent = function()
    { 
      element(by.xpath("//*[@id='verticalRadioGrp']/div/select")).all(by.tagName("option")).then(function(options)
    { 
       options[3].click();
    });

【问题讨论】:

  • 可能是您有错字并正在寻找AreasForPresents[3](即缺少a)?
  • 你能发布这个运行的文档(html)吗?
  • @Garr Godfrey 很遗憾不是,因为它是一个公共组织。

标签: javascript selenium protractor


【解决方案1】:

您的代码似乎无法处理承诺。

var AresForPresents = element(by.xpath("//*[@id='verticalRadioGrp']/div/select")).all(by.tagName("option"));
this.SelectAreaToGetPresent = function()
{ 
    AresForPresents.then((options)=> { options[3].click(); }); 
};

否则,您需要async/await

var AresForPresents;
this.SelectAreaToGetPresent = async ()=>
{ 
    AresForPresents = await element(by.xpath("//*[@id='verticalRadioGrp']/div/select")).all(by.tagName("option")); 
    AresForPresents[3].click();
};

【讨论】:

    猜你喜欢
    • 2017-04-13
    • 2022-11-17
    • 1970-01-01
    • 2017-07-12
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多