【问题标题】:Webdriver get list of children and filter it with xpathWebdriver 获取子列表并使用 xpath 过滤它
【发布时间】:2019-03-26 11:27:41
【问题描述】:

我正在获取我的根 WebElement 的子项列表,如下所示:

WebElement listOfAddedBuildings = driver.getWebElement("//*[@id=\"right-list\"]/div/mat-list", 10);
List<WebElement> child = listOfAddedBuildings.findElements(By.xpath(".//*"));

但是现在它返回一个元素列表,包括我不需要的元素,我如何修改它以过滤并仅返回包含“h3”标签但不包含“mat-icon”标签的元素,目前我只是这样做,但感觉不是最好的方式:

if (xPath.contains("h3") && !xPath.contains("mat-icon"))

谢谢。

【问题讨论】:

  • 您想要不包含mat-iconh3 吗?或者你想要任何个有h3但没有mat-icon的孩子?

标签: selenium testing selenium-webdriver xpath automation


【解决方案1】:

如果您想要具有h3 后代但没有mat-icon 后代的孩子,请尝试:

List<WebElement> child = listOfAddedBuildings.findElements(By.xpath("./*[.//h3 and not(.//mat-icon)]"));

【讨论】:

  • 感谢您的详细回复,帮助很大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 2018-04-25
  • 2018-07-15
相关资源
最近更新 更多