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
.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With