基本上,我想获取网页上的所有 WebElement。我正在使用下面的内容,它对于静态 HTML 页面运行良好。
List<WebElement> all = wd.findElements(By.cssSelector("*"));
但是在 Angular 页面上,我无法获取所有元素(由于 HTML 页面中使用的指令)。
页面查看源代码是这样的:
<html>
<body>
<app-root></app-root>
</body>
</html>
Endgame 是创建一个在页面上生成定位器的工具,我使用 Java 和 selenium-wd 来实现它。如果有更好的方法,请提出。
最佳答案
查询页面的所有元素,然后使用应用程序鉴别器(通常是 app-
)对 Angular 元素进行排序。像这样的事情:
const allElements = [...document.querySelectorAll('*')];
const angularElements = allElements.filter(el => el.tagName.toLowerCase().startsWith('app-'));
console.log(
angularElements.length + ' Angular elements found : ' + angularElements
.map(el => el.tagName.toLowerCase()).join(', ')
);
<app-root>
<app-component>
<app-child-one></app-child-one>
<app-child-two></app-child-two>
</app-component>
</app-root>
<div></div>
<mat-select></mat-select>
关于java - 如何使用 Selenium Webdriver 从 Angular 应用程序获取所有 WebElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56686043/