只是想共享:如果您处于某个部分中没有类别的情况,并且该特定div的值不能进行硬编码和更改,则使用Protractor E2E框架,我设法使用此方法定位特定的DIV:
添加一个没有每个元素的类HTML的示例
<div class="row">
<div class="page_banner">A Dude's Profile</div>
<div class="profile_details">
<div class="profile_name">
<h3>Tony Adams</h3>
</div>
<div>ta@bogus.com</div>
<div>0883424324</div>
</div>
</div>
在您需要说明存在唯一手机号码的情况下,因此该值不一致。
function mobileNumberAssert() {
element.all(by.css('.profile_details'))
.get(1) // number of divs in css
.getText()
.then(function(textFoundInCss) {
if(textFoundInCss > 10) {
return true;
console.log('there is a mobile number present with 10 digits');
} else {
return false;
}
});
}
用于调试您可以安装登录" textfoundincss"来定位该特定div。
,因为您似乎正在根据文本寻找一个元素,您可以使用 by.cssContainingText()
使其更容易。
const el = element(by.cssContainingText('div', `Text I'm looking for`);
el.isPresent().then(isPresent => {
if (isPresent) {
// do something ...
} else {
return false;
}
}