How to find the pseudo element of an web element in geb

94 views
Skip to first unread message

Dwiparna Pal

unread,
Mar 21, 2018, 12:55:00 PM3/21/18
to geb-...@googlegroups.com
Hi,

I need to find a pseudo element on my page. The html code looks like 

<label for="abc"> == $0
::before
"some text" 
<a target="_blank" href="/abcd">something</a></label>

My element is located by the pseudo element ::before. How I could select my element in Geb?

Thanks,
Dwiparna

Alexander Kriegisch

unread,
Mar 21, 2018, 9:03:58 PM3/21/18
to geb-...@googlegroups.com
Your sample is neither valid HTML nor valid CSS. Would you mind giving a real example?
--
Alexander Kriegisch
https://scrum-master.de

Alexander Kriegisch

unread,
Mar 24, 2018, 12:48:39 AM3/24/18
to geb-...@googlegroups.com
Hey Dwiparna,

thank you so much for not answering anymore. I regard it as rude. You
cannot ask for help and then ignore answers.

Anyway, I bumped into this in another context. Now I guess I know what
you want. It is about CSS pseudo elements, right? For example:

======================================================================

<html style="font-family: sans-serif">
<head>
<style>
label::before {
content: "some text";
}
</style>
</head>
<body>
<label for="abc"> == $0
<a target="_blank" href="/abcd">something</a></label>
</body>
</html>

======================================================================

Unfortunately Selenium (WebDriver) does not support that. Tickets
requesting support have even been closed as "won't implement". so all
you are left with is using JavaScript from within Geb like this:

======================================================================

package de.scrum_master.stackoverflow

import geb.spock.GebReportingSpec

class PesudoElementIT extends GebReportingSpec {
def "Get text from CSS pseudo-element ::before"() {
given:
go "file:///C:/my/path/dateformat-timezone.htm"
def jsCode = '''
return window
.getComputedStyle(document.querySelector('label'),'::before')
.getPropertyValue('content')
'''

when:
def pseudoElementContent = js.exec(jsCode)[1..-2]

then:
pseudoElementContent == "some text"
}
}

======================================================================

BTW, if you are wondering why you need "[1..-2]", it is because the text
is returned with surrounding double quotes which need to be cut off in
order to get just the text content.

Regards
--
Alexander Kriegisch
https://scrum-master.de


Alexander Kriegisch schrieb am 22.03.2018 08:03:

> Your sample is neither valid HTML nor valid CSS. Would you mind giving a real
> example?
>
>
Reply all
Reply to author
Forward
0 new messages