使用 Protractor ,我的目标是在给定名称值的情况下单击更新按钮,这是唯一的。
<div class="Employee">
<div class="address">
<button class="street" value="update"/>
</div>
<div class="profile">
<div class="name" value="Joe Smith"/>
</div>
</div>
<div class="Employee">
<div class="address">
<button class="street" value="update"/>
</div>
<div class="profile">
<div class="name" value="Jane Smith"/>
</div>
</div>
因此尝试查找所有“Employee”元素以找到所有父元素,然后过滤以查找哪个“Employee”元素具有值为“Jane Smith”的特定子元素。然后使用该父元素单击“更新”按钮。
element.all(by.css('[class="Employee"]')).filter(function (elem,index) {
if(elem.element(by.css('[value="Jane Smith"]')).isPresent()){
return elem
}
}).then(function (elem) {
elem[0].element(by.css('[value="update"]')).click()
})
问题是在“if”语句中,它总是等于 true,我不确定为什么。
也许有另一种方法可以在不使用 xpaths 的情况下解决这个问题,因为这个环境总是在变化
最佳答案
我不经常在我们的应用程序中使用 filter
,所以有点盲目尝试,但我相信您需要一个额外的 return
语句。看看这是否适合您:
element.all(by.css('[class="Employee"]')).filter(function (elem,index) {
return elem.element(by.css('[value="Jane Smith"]')).isPresent().then(function(val) {
return val == true; // or val === true if you prefer
});
}).then(function (items) {
console.log(items.length) // just to check the number is what you expect
items[0].element(by.css('[value="update"]')).click()
});
关于javascript - 无法使用 Protractor 定位同一祖 parent 的孙元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38465039/