Select only the displayed element matching a selector

27 views
Skip to first unread message

Ben Frey

unread,
Aug 24, 2020, 10:54:31 AM8/24/20
to Geb User Mailing List
I have a page that has one visible instance of a radio button, but the CSS selector (using id$=) returns more than one. How can I select only the visible one so I can use click()?

Michael Kutz

unread,
Aug 24, 2020, 11:11:49 AM8/24/20
to Geb User Mailing List
The simplest solution would be to find a selector that is more precise as it would avoid creating unwanted Navigator objects. So if you can come up with something like
$("#radio1.visible").click()
It would be the best way.

The fallback here would be to get a list of all Navigators and filter them using Groovy:
$("#radio1").find(it.isDisplayed()).click()

Am Mo., 24. Aug. 2020 um 16:54 Uhr schrieb Ben Frey <sparta...@gmail.com>:
I have a page that has one visible instance of a radio button, but the CSS selector (using id$=) returns more than one. How can I select only the visible one so I can use click()?

--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/adc6b76c-a7e5-429a-a984-44754867b385n%40googlegroups.com.

Ben Frey

unread,
Aug 24, 2020, 11:29:30 AM8/24/20
to Geb User Mailing List
Hmm, I'm pretty new to CSS selectors, but since I'm having to use the "id ends with" selector syntax (e.g. $('[id$=endOfId]')), adding .visible ($('[id$=endOfId].visible')) doesn't seem to work; I get an empty Navigator. I guess I'll have to do the fallback, unless there's something I'm missing.

Michael Kutz

unread,
Aug 24, 2020, 11:49:44 AM8/24/20
to Geb User Mailing List
I was suggesting to find a class to narrow the selector to the actually visible radio button.
As visibility is usually handled via CSS, there should be some class (e.g. one named "visible-radio-button") that you can use in your selector.

Ben Frey

unread,
Aug 24, 2020, 12:06:41 PM8/24/20
to Geb User Mailing List
Unfortunately this element doesn't have any CSS classes attached to it. Just id, type, title, and value.

Michael Kutz

unread,
Aug 24, 2020, 12:18:12 PM8/24/20
to Geb User Mailing List
Then the fallback should still work.

Ben Frey

unread,
Aug 24, 2020, 12:25:25 PM8/24/20
to Geb User Mailing List
Yup, it looks to be. Thanks!
Reply all
Reply to author
Forward
0 new messages