::before CSS element is not discovered by seelnium

376 views
Skip to first unread message

Rohit

unread,
May 9, 2020, 11:53:07 AM5/9/20
to Selenium Users
Hi guys,
The element i want to find is selected at ::before html code. However selenium isnt able to  extract the spath. I tried to capture using.

driver.findElement(By.xpath("//a[@class='list_filter_toggle icon-filter btn btn-icon']/*")).click();
driver.findElement(By.id("sys_user_filter_toggle_image")).click();

<a role="button" class="list_filter_toggle icon-filter btn btn-icon" tabindex="0" title="Show / hide filter" id="sys_user_filter_toggle_image" aria-controls="sys_userfilterdiv" aria-expanded="true" data-original-title="Show / hide filter"><span class="sr-only">Show / hide filter</span></a>
:before

Nothing semed to b working. also tried action keyword but its failing. Need help please :).

Thanks
Rohit

Ankit Mundra

unread,
May 10, 2020, 2:50:14 AM5/10/20
to Selenium Users
Hi Rohit

::before and ::after are css pseudo elements. And Selenium cannot identify CSS pseudo elements. You should try using JavaScriptExecutor to perform the actions you want.
or You can try to perform a context click on the element and then try. But ensure that you use CSS Selector to identify the CSS Pseudo Elements. Xpath is not going to work for these elements at all.

Regards
Ankit M

Rohit Srivastava

unread,
May 10, 2020, 5:35:44 AM5/10/20
to seleniu...@googlegroups.com
Thanks Ankit.
Can you please let me know how is it done? I am new to selenium.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/31a8c0d6-9cda-4378-abbf-3037c1942bd7%40googlegroups.com.

Rohit Srivastava

unread,
May 10, 2020, 12:40:06 PM5/10/20
to seleniu...@googlegroups.com
I tried Java executer but the problem is the element isn't getting captured...


This is the region where the element is


<a role="button" class="list_filter_toggle icon-filter btn btn-icon" tabindex="0" title="Show / hide filter" id="sys_user_filter_toggle_image" aria-controls="sys_userfilterdiv" aria-expanded="true" data-original-title="Show / hide filter"><span class="sr-only">Show / hide filter</span></a>
::before

And when I point at "before" I see the required element higighted...but Everytime I try the elent isn't captured using css..there isn't any content type field available for using css sleector..
I am confused... :(

On Sun, May 10, 2020, 8:50 AM Ankit Mundra <ankitm...@gmail.com> wrote:
--

Josh Abrahamsen

unread,
May 10, 2020, 12:54:57 PM5/10/20
to seleniu...@googlegroups.com
This should work. I've seen ::before in some markup but I've never seen where you specifically need to point to only that and not the element above it.

By.CssSelector([title='Show / hide filter'])

Or

By.CssSelector([id='sys_user_filter_toggle_image'])


Rohit Srivastava

unread,
May 10, 2020, 1:17:52 PM5/10/20
to seleniu...@googlegroups.com
Hi,
I used

WebElement element = driver.findElement(By.cssSelector("a#sys_user_filter_toggle_image"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);




Error"

FAILED: f
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"a#sys_user_filter_toggle_image"}
  (Session info: chrome=81.0.4044.138)

Rohit Srivastava

unread,
May 10, 2020, 1:23:34 PM5/10/20
to seleniu...@googlegroups.com
image.png

The filter is what i wish to click.

Alvaro Jose Garcia Cohen

unread,
May 10, 2020, 4:59:20 PM5/10/20
to seleniu...@googlegroups.com
Hi Rohit!

Try this

driver.findElement(By.xpath("//a[@id=' sys_user_filter_toggle_image']/span[text() = ' Show / hide filter ']")).click();

regards!


Alvaro José García Cohen
aj.ga...@gmail.com


Rohit Srivastava

unread,
May 11, 2020, 3:03:07 AM5/11/20
to seleniu...@googlegroups.com
FAILED: f
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@id=' sys_user_filter_toggle_image']/span[text() = ' Show / hide filter ']"}
  (Session info: chrome=81.0.4044.138)


Not working/

Joe Ward

unread,
May 11, 2020, 3:05:46 AM5/11/20
to seleniu...@googlegroups.com
I think instead of trying code snippets you're being sent from the group and reporting back whether or not they work your time would be better spent studying things like:


I would also suggest if you're having a lot of trouble with this task talking to somebody about them introducing a better locator for you into the application is a worthy thing to do.

Rohit Srivastava

unread,
May 13, 2020, 3:47:31 PM5/13/20
to seleniu...@googlegroups.com
Hi,
I am still stuck. Tried several things
Any help would be appreciated.

Thanks
Rohit

Joe Ward

unread,
May 14, 2020, 7:30:59 AM5/14/20
to seleniu...@googlegroups.com
How are you stuck? What several things have you tried? You’ve got to help us out a bit here too you know. 🤣

Rohit Srivastava

unread,
May 14, 2020, 11:17:29 AM5/14/20
to seleniu...@googlegroups.com
:)
I tried everything from mail trail.
Let me post my question again.

Hi guys,
The element i want to find is selected at ::before html code. However selenium isnt able to  extract the spath. I tried to capture using.

driver.findElement(By.xpath("//a[@class='list_filter_toggle icon-filter btn btn-icon']/*")).click();
driver.findElement(By.id("sys_user_filter_toggle_image")).click();


This was the code.


<a role="button" class="list_filter_toggle icon-filter btn btn-icon" tabindex="0" title="Show / hide filter" id="sys_user_filter_toggle_image" aria-controls="sys_userfilterdiv" aria-expanded="true" data-original-title="Show / hide filter"><span class="sr-only">Show / hide filter</span></a>
::before

Nothing seemed to b working. also tried action keyword but its failing. Need help please :).

Tried searching via css selector
JavaScript executor. I seem to be missing something.






Ankit Mundra

unread,
May 15, 2020, 2:08:34 AM5/15/20
to Selenium Users
Hi Rohit

Did you try the link provided by '@That Guy' on A Guide to Working with Shadow DOM using Selenium. Did you read it through? Is it really applicable in your scenario? Are you sure your element is a CSS psuedo element? On the UI is the element you are pointing actually taking the click? Or there is some other element that is suppose to receive the click?

also, take a look the following Stackoverflow questions ::


And there are many such solutions.

Regards
Ankit M
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages