如何使用Selenium Webdriver从有角度的应用程序中获取所有WebElement?

时间:2019-06-20 12:29:57

标签: java angular selenium-webdriver

基本上,我想在网页上获取所有WebElement。我正在使用下面的方法来处理静态HTML页面。

List<WebElement> all = wd.findElements(By.cssSelector("*"));

但是在有角页面上,我无法获取所有元素(由于HTML页面中使用了指令)。

页面“查看源代码”是这样的:

<html>
<body>
<app-root></app-root>
</body>
</html>

Endgame是要创建一个在页面上生成定位符的工具,我为此使用Java和selenium-wd。如果有更好的方法,请提出建议。

1 个答案:

答案 0 :(得分:0)

查询页面的所有元素,然后使用应用程序判别器(通常为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>