Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to find child (not all descendants) elements via an existing element (not xpath)
    primarykey
    data
    text
    <p>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.</p> <p>For example, given the following HTML snippet (you may recognize this as a bootstrap dropdown menu).</p> <pre><code>&lt;div class="dropdown"&gt; &lt;ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu"&gt; &lt;li&gt; &lt;a tabindex="-1" href="#"&gt;Action&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a tabindex="-1" href="#"&gt;Another action&lt;/a&gt; &lt;/li&gt; &lt;li&gt; &lt;a tabindex="-1" href="#"&gt;Something else here&lt;/a&gt; &lt;/li&gt; &lt;li class="divider"&gt;&lt;/li&gt; &lt;li class="dropdown-submenu"&gt; &lt;a tabindex="-1" href="#"&gt;More options&lt;/a&gt; &lt;ul class="dropdown-menu"&gt; &lt;li&gt;&lt;a tabindex="-1" href="#"&gt;Second level link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a tabindex="-1" href="#"&gt;Second level link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a tabindex="-1" href="#"&gt;Second level link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a tabindex="-1" href="#"&gt;Second level link&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a tabindex="-1" href="#"&gt;Second level link&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>I already have the existing WebElement to the dropdown menu</p> <pre><code>WebElement dropdown = driver.findElementBy(By.cssSelector(".dropdown &gt; .dropdown-menu"); </code></pre> <p>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</p> <pre><code>List&lt;WebElement&gt; menuItems = dropdown.findElements(By.cssSelector(" &gt; li &gt; a"); </code></pre> <p>assuming it would result in a final selector being built</p> <pre><code>.dropdown &gt; .dropdown-menu &gt; li &gt; a </code></pre> <p>However, this throws an exception</p> <pre><code>Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: An invalid or illegal string was specified </code></pre> <p>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.</p> <pre><code>List&lt;WebElement&gt; menuItems = dropdown.findElements(By.cssSelector("li &gt; a"); </code></pre> <p>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?</p> <p>XPATH is not an allowable solution.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload