Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

how to find child (not all descendants) elements by relative css selector #6050

Closed
lukeis opened this issue Mar 4, 2016 · 7 comments
Closed

Comments

@lukeis
Copy link
Member

lukeis commented Mar 4, 2016

Originally reported on Google Code with ID 6050

What steps will reproduce the problem?
1. Find an element by whatever means and assign it to a variable (e.g. via css ".dropdown
> .dropdown-menu")
2. Using that found element, try to find its direct children via css (e.g. "> li >
a" resulting in a total css selector of ".dropdown > .dropdown-menu > li > a")

Expected result:

Expect the children of the found element not an exception thrown. More Details below.

----------

Selenium version: 2.31
OS: Windows 7
Browser: Firefox
Browser version: 22

----------

See: http://stackoverflow.com/questions/18066578/how-to-find-child-not-all-descendants-elements-via-an-existing-element-not-xp

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).

<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 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 count of menuItems is 10 instead of 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? Is there a way to reference the current node
in the css selector text e.g. "&this& > li > a"?

Reported by aaron.loes on 2013-08-06 15:49:11

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

My apologies but i messed up the original ticket (#6049) ... please feel free to close
THAT ticket.

Reported by aaron.loes on 2013-08-06 15:50:29

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Issue 6049 has been merged into this issue.

Reported by a.u.savchuk on 2013-08-06 17:13:08

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

It should be allowed with :scope selector, see [1].
But I'm not sure about old browsers.

[1] http://updates.html5rocks.com/2013/03/What-s-the-CSS-scope-pseudo-class-for

Reported by barancev on 2013-08-08 07:21:51

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by barancev on 2013-08-08 22:33:41

  • Labels added: Type-Enhancement, Component-WebDriver
  • Labels removed: Type-Defect

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

It is not working with Firefox 27.0.1 with Selenium 2.40.0 and firefox driver 27.0.1.
  Here is what I did:

        def rows = browserTableRows()
        def row0 = rows[0];
        println "row0 is " + row0.getClass()
        def tds = row0.find(":scope > td")
        println "tds is " + tds.getClass()

The same this like:

        def rows = browserTableRows()
        def row0 = rows[0];
//        println "row0 is " + row0.getClass()
//        def tds = row0.find(":scope > td")
        def tds = row0.children("td")
        println "tds is " + tds.getClass()

does work on the same content

Here is the stack traceback:

Build info: version: '2.40.0', revision: '4c5c0568b004f67810ee41c459549aa4b09c651e',
time: '2014-02-19 11:13:01'
System info: host: 'Bretts-MacBook-Pro.local', ip: '192.169.1.203', os.name: 'Mac OS
X', os.arch: 'x86_64', os.version: '10.9.2', java.version: '1.6.0_65'
Session ID: dace2178-9858-3a49-83c2-aa6d00ff6cba
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox,
rotatable=false, locationContextEnabled=true, version=27.0.1, cssSelectorsEnabled=true,
databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false,
webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
org.openqa.selenium.InvalidSelectorException: The given selector :scope > td is either
invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: An invalid or illegal selector was specified
Command duration or timeout: 10 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.40.0', revision: '4c5c0568b004f67810ee41c459549aa4b09c651e',
time: '2014-02-19 11:13:01'
System info: host: 'Bretts-MacBook-Pro.local', ip: '192.169.1.203', os.name: 'Mac OS
X', os.arch: 'x86_64', os.version: '10.9.2', java.version: '1.6.0_65'
Session ID: dace2178-9858-3a49-83c2-aa6d00ff6cba
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox,
rotatable=false, locationContextEnabled=true, version=27.0.1, cssSelectorsEnabled=true,
databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false,
webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:573)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:187)
    at org.openqa.selenium.remote.RemoteWebElement.findElementsByCssSelector(RemoteWebElement.java:240)
    at org.openqa.selenium.By$ByCssSelector.findElements(By.java:436)
    at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:163)
    at geb.navigator.NonEmptyNavigator.find_closure1(NonEmptyNavigator.groovy:44)
    at groovy.lang.Closure.call(Closure.java:423)
    at groovy.lang.Closure.call(Closure.java:439)
    at geb.navigator.NonEmptyNavigator.find(NonEmptyNavigator.groovy:43)
    at com.canoga.nms.web.pkg.pkg9145e100g.ui.device.devicebrowser.DeviceBrowserPage.selectDeviceBrowserRowWithName(DeviceBrowserPage.groovy:54)
    at geb.Browser.methodMissing(Browser.groovy:168)
    at geb.spock.GebSpec.methodMissing(GebSpec.groovy:51)
    at com.canoga.nms.web.pkg.pkg9145e.v1.ui.device.devicebrowser.DeviceBrowserManualCreateSpec.Select
9145E100G-D91-M-0 NID Template(DeviceBrowserManualCreateSpec.groovy:70)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: The given
selector :scope > td is either invalid or does not result in a WebElement. The following
error occurred:
InvalidSelectorError: An invalid or illegal selector was specified

Reported by bbergquist1962 on 2014-03-10 20:50:12

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

We can't (won't) implement this on Selenium side, locators are standard and we use what
a browser provides.

Reported by barancev on 2014-10-01 19:52:48

  • Status changed: NotFeasible
  • Labels removed: Status-Untriaged

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by luke.semerau on 2015-09-17 18:17:41

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 4, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant