gpt4 book ai didi

selenium - Protractor 查找父元素

转载 作者:行者123 更新时间:2023-12-03 05:03:49 27 4
gpt4 key购买 nike

<label class="radio inline check">

<input class="input ng-new ng-valid" name="BookType" required="" type="radio">

<!---->

<!---->
Fiction
</label>

<label class="radio inline check">

<input class="input ng-new ng-valid" name="BookType" required="" type="radio">

<!---->

<!---->
NonFiction
</label>

<label class="radio inline check">

<input class="input ng-new ng-valid" name="BookTypeReal" required="" type="radio">

<!---->

<!---->
Fiction
</label>

<label class="radio inline check">

<input class="input ng-new ng-valid" name="BookTypeReal" required="" type="radio">

<!---->

<!---->
Fantasy
</label>

http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.filter

如果我使用

element.all(locator).filter(filterFn) 

返回的文本为空。

如何转到父元素<label>获取文本?

label[class="radio inline check"] 

返回 60 个元素,其中多个 getText 将返回相同的文本,因此它不会是唯一的。

input[name="BookType"] 

返回两个元素,其中每个元素都是唯一的。

最佳答案

我使用 TypeScript 使用了以下方法:

import {by, ElementFinder} from 'protractor';

export interface ElementDescriptor {
tag?: string;
cssClass?: string;
}

async function matches(element: ElementFinder, parentDescription: ElementDescriptor): Promise<boolean> {
const promises = [];
if (parentDescription.tag) {
promises.push(element.getTagName());
}
if (parentDescription.cssClass) {
promises.push(element.getAttribute('class'));
}

let i = 0;
const results: string[] = await Promise.all(promises);

return (!parentDescription.tag || results[i++] === parentDescription.tag)
&& (!parentDescription.cssClass || results[i++].split(' ').includes(parentDescription.cssClass));
}

export async function findParent(element: ElementFinder, parentDescription: ElementDescriptor): Promise<ElementFinder> {
let parent: ElementFinder;
do {
parent = element.element(by.xpath('..'));
} while ((await parent.getTagName()) !== 'html' && !(await matches(parent, parentDescription)));
return parent;
}

关于selenium - Protractor 查找父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49134582/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com