简体   繁体   English

如果值/标题包含文本,如何 select 元素

[英]How to select a element if value/title contains a text

I have been trying to find out on how I can grab the element if a value contains "6 months".如果值包含“6 个月”,我一直在尝试找出如何获取该元素。 Here is few example and as we can see all of these values has the word 6 months and this is different scenarios of course so I don't want to click all of them but I gave example of different banktype that I need to click这是几个例子,我们可以看到所有这些值都有 6 个月这个词,这当然是不同的场景,所以我不想点击所有这些,但我给出了我需要点击的不同银行类型的例子

<input type="button" aria-label="AMEX 6 months Extended Payment Plan" title="AMEX 6 months Extended Payment Plan" value="AMEX 6 months Extended Payment Plan" class="pm-button pp-content-btn pm-button-half">

<input type="button" aria-label="DBS IPP 6 months 0%" title="DBS IPP 6 months 0%" value="DBS IPP 6 months 0%" class="pm-button pp-content-btn pm-button-half">

<input type="button" aria-label="UOBSG IPP 6 months 0%" title="UOBSG IPP 6 months 0%" value="UOBSG IPP 6 months 0%" class="pm-button pp-content-btn pm-button-half">

and I wonder if its possible to click on a element if the value/title contains the word "6 months" inside it?我想知道如果值/标题中包含“6个月”这个词,是否可以点击一个元素?

EDIT: Using protractor for testing编辑:使用 protractor 进行测试

You can achieve this using xpaths with the contains function like so:您可以使用包含 function 的 xpath 来实现此目的,如下所示:

element(by.xpath('//input[contains(@title,"6 months")]');

The contains function is very useful for locating elements which contain certain text either in an attribute or in the node itself. 包含 function对于定位在属性或节点本身中包含某些文本的元素非常有用。

since you can't use the querySelectorAll, you might find this function helpful:由于您不能使用 querySelectorAll,您可能会发现此 function 很有帮助:

in case you don't depend on the element name:如果您不依赖元素名称:

FindByAttributeValue("Attribute-Name", "Attribute-Value");

or in case you do:或者如果你这样做:

FindByAttributeValue("Attribute-Name", "Attribute-Value", "div");

and the full function implementation:以及完整的 function 实现:

function FindByAttributeValue(attribute, value, element_type){
    element_type = element_type || "*";
    var All = document.getElementsByTagName(element_type);
    for (var i = 0; i < All.length; i++){
        if (All[i].getAttribute(attribute) == value){
            return All[i];
        }
    }
}

full answer came from HERE完整的答案来自这里

That's exactly what by.cssContainingText does (partial text matcher in conjunction with css)这正是by.cssContainingText所做的(部分文本匹配器与 css 结合使用)

let elem = element(by.cssContainingText('[style]', '6 months'));

REVISION修订

1 way for doing what you need, but very inefficient... You may use .filter against elementArrayFinder several times until you narrow down the results to the element you need一种做你需要的方法,但效率很低......你可以对 elementArrayFinder 使用.filter多次,直到你将结果缩小到你需要的元素

But I'd look for the element you need by 2 parameters但我会通过 2 个参数来寻找你需要的元素

let $elem = (bank, range) => element(by.xpath(`//input[contains(.,"${bank}") and contains(.,"${range}")]`));

Correct the xpath as needed.根据需要更正 xpath。 But essentially it looks for an input element that partially matches anything (attribute, text etc) with first parameter AND the second one但本质上,它会寻找一个input元素,该元素与第一个参数和第二个参数部分匹配任何内容(属性、文本等)

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

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