Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How run a Query on an Element?

Issue and how I would do it with the plain Selenium API

I often need to query an element on the page (say with cssSelector().element), use it, and then want to find a descendent element. With the plain Selenium API, I would write something like:

val foo = webDriver.findElement(By.cssSelector(".foo"))
val bar = foo.findElement(By.cssSelector(".bar"))

How can I do this with the ScalaTest Selenium DSL? I can get the "foo" element with:

val foo = cssSelector(".foo").element

But then how to get "bar" from "foo"? Of course, I could just use the Selenium API in that case (i.e. val bar = foo.underlying.findElement(By.cssSelector(".bar"))), but end up with a WebElement instead of a ScalaTest Element.

Workaround running another query

For now, I just run the query all over again, as shown below, but find this verbose, less clear, and not always equivalent to just looking for elements under an element.

val fooSelector = cssSelector(".foo")
val foo = fooSelector.element
val boo = cssSelector(fooSelector.queryString + " .bar").element
like image 464
avernet Avatar asked Nov 13 '22 08:11

avernet


1 Answers

The cssSelector method creates a CssSelectorQuery object. You can pass that object as an argument to the find method:

val foo = find(cssSelector(".foo")).get
like image 176
Zoltán Avatar answered Dec 18 '22 09:12

Zoltán