简体   繁体   English

如何通过现有元素(而不是xpath)查找子元素(不是所有后代元素)

[英]how to find child (not all descendants) elements via an existing element (not xpath)

I am trying to find the children of an already found element. 我试图找到已经找到的元素的孩子。 The problem I am having is that I can seem to only get all descendants of an existing element. 我遇到的问题是我似乎只能得到现有元素的所有后代。

For example, given the following HTML snippet (you may recognize this as a bootstrap dropdown menu). 例如,给定以下HTML片段(您可能会将其识别为引导程序下拉菜单)。

<div class="dropdown">
  <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
    <li>
      <a tabindex="-1" href="#">Action</a>
    </li>
    <li>
      <a tabindex="-1" href="#">Another action</a>
    </li>
    <li>
      <a tabindex="-1" href="#">Something else here</a>
    </li>
    <li class="divider"></li>
    <li class="dropdown-submenu">
      <a tabindex="-1" href="#">More options</a>
      <ul class="dropdown-menu">
        <li><a tabindex="-1" href="#">Second level link</a></li>
        <li><a tabindex="-1" href="#">Second level link</a></li>
        <li><a tabindex="-1" href="#">Second level link</a></li>
        <li><a tabindex="-1" href="#">Second level link</a></li>
        <li><a tabindex="-1" href="#">Second level link</a></li>
      </ul>
    </li>
  </ul>
</div>

I already have the existing WebElement to the dropdown menu 我已经在下拉菜单中使用了现有的WebElement

WebElement dropdown = driver.findElementBy(By.cssSelector(".dropdown > .dropdown-menu");

And I am trying to get a handle to all the child li's of the outer most menu. 而我正试图找到最外层菜单中所有孩子的手柄。 Originally I was doing the following 最初我在做以下事情

List<WebElement> menuItems = dropdown.findElements(By.cssSelector(" > li > a");

assuming it would result in a final selector being built 假设它会导致构建最终选择器

.dropdown > .dropdown-menu > li > a

However, this throws an exception 但是,这引发了一个例外

Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: An invalid or illegal string was specified

Doing the following doesn't throw an exception but returns all descendants ... not just the children. 执行以下操作不会抛出异常,但会返回所有后代...而不仅仅是子项。 The cound of menuItems is 10 instead of 5. menuItems的结果是10而不是5。

List<WebElement> menuItems = dropdown.findElements(By.cssSelector("li > a");

So I guess my ultimate question is there a way to use By.cssSelector to get just the direct children of an already found node? 所以我想我的最终问题是有一种方法可以使用By.cssSelector来获取已经找到的节点的直接子节点吗?

XPATH is not an allowable solution. XPATH不是一个允许的解决方案。

What about change the logic a bit? 如何改变逻辑呢?

You have a WebElement called dropdown , but it is defined as class dropdown-menu in the DOM. 您有一个名为dropdownWebElement ,但它在DOM中定义为class dropdown-menu Why not define dropdown as div.dropdown instead of ul.dropdown-menu ? 为什么不将dropdown定义为div.dropdown而不是ul.dropdown-menu Then your issue can be solved. 那你的问题就可以解决了。

WebElement dropdown = driver.findElement(By.cssSelector(".dropdown");
List<WebElement> menuItems = dropdown.findElements(By.cssSelector(".dropdown-menu[role='menu'] > li > a");
List<WebElement> subMenuItems = dropdown.findElements(By.cssSelector(".dropdown-submenu li > a");

为什么不把它们结合起来?

List<WebElement> menuItems = driver.findElements(By.cssSelector(".dropdown > .dropdown-menu  > li > a");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM