无法检索链接&点击它 - 量角器

时间:2017-05-16 06:06:08

标签: javascript automation jasmine protractor

我正在尝试将网页的所有网页链接放在一个阵列中。我想点击文本匹配的链接,因为网页中网址链接的位置不固定。 我的下面代码失败并出现错误:

失败:索引越界。试图访问index:15处的元素,但只有0个元素匹配定位器By(css selector,.entity.element.a)

var result_links =element.all(by.css('.entity.element.a'));  
    for (var i=0;i<22;i++){
        result_links.get(i).getText().then(function(text){
        console.log("The fetched links are "+text);
        if (text==="Xtreme Testing -explorers 12345"){
            result_links.get(i).click();
        }       
        })
    }

我尝试使用Javascript的result_links.length属性,但奇怪的是我没有得到它的数组编号因此我必须静态分配数字22的原因我正在寻找的网络链接将始终在0-22之间< / p>

以下代码的输出为:

The fetched links are Stick urls
The fetched links are Budgets 27167678
The fetched links are www.xyztyi.com
The fetched links are YT its yours
The fetched links are wwww.thytghj.com
The fetched links are Engage stick
The fetched links are Methods 156788.com
The fetched links are Stithgyuji 287878
The fetched links are Hello 679u
The fetched links are Johns 766788
The fetched links are www.gtyyuuhjhj
The fetched links are yuyuuyukuku
The fetched links are Stanfods 777686y88
The fetched links are www.xyz.com
The fetched links are Xtreme Testing -explorers 12345

2 个答案:

答案 0 :(得分:0)

您可以使用by.css()by.linktext("Xtreme Testing -explorers 12345")by.partialLinkText("Xtreme Testing -explorers 12345")

而不是by.cssContainingText(".entity.element.a","Xtreme Testing -explorers 12345")

其他替代解决方案:

var result_links =element.all(by.css('.entity.element.a')); 
result_links.getText().then(function(linkList){
    var index=text.indexOf("Xtreme Testing -explorers 12345");
    result_links.get(index).click();  
});

var result_links =element.all(by.css('.entity.element.a')); 
result_links.filter(function(link){
  return link.getText().then(function(linkText){
       return linkText == "Xtreme Testing -explorers 12345";
  });
}).first().click();

答案 1 :(得分:-1)

如果不是强制要求采用数组方法,那么如上所述,您可以使用linktext或partiallinktext,它应该可以工作。 或者你也可以尝试indexOf方法,即

var result_links =element.all(by.css('.entity.element.a'));  
    for (var i=0;i<22;i++){
        result_links.get(i).getText().then(function(text){
        console.log("The fetched links are "+text);
        var index=text.indexOf("Xtreme Testing -explorers 12345");
        result_links.get(index).click();       
        })
    }