Suppose I'm looking for some div elements under #myPage element. My goal is to use CSS selectors and limit the search only to #myPage descendants.
Using Selenium XPath locators I can write something like this:
WebElement page = driver.findElement(new By.ById("myPage"));
....
List<WebElement> item = page.findElements(new By.ByXPath(".//div"));
However, trying to use CSS yields all divs in documents, not only descendants of #myPage:
WebElement page = driver.findElement(new By.ById("myPage"));
....
List<WebElement> item = page.findElements(new By.ByCssSelector("div"));
The big difference is the .//
prefix that makes the XPath expression relative. I couldn't find similar property in CSS syntax, and I wonder if it's even possible.
P.S.
I know I can use #myPage > div
expression, but then I couple the page lookup operation with the lookup of its descendants, which is not always desirable.