Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver select element that has ::before

I have 2 elements that have the same attributes but shown one at a time on the page (When one is shown, the other disappears).The only difference between the two is that the element which is displayed will have the '::before' selector. Is it possible to use an xpath or css selector to retrieve the element based on its id and whether or not it has ::before

enter image description here

like image 467
Jeremy Borg Avatar asked Apr 21 '15 12:04

Jeremy Borg


People also ask

What is :: before in XPath?

::before is a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML). While the end result is not actually in the DOM, it appears on the page as if it is.

What is :: before in selenium?

As the name suggests, the ::before pseudo-element in CSS is used to add special CSS styling before the content of any element.

How do you select pseudo element in selenium?

What is a pseudo-element? These are called pseudo-elements, and they are CSS keywords added to the element's selector which allow you to style only specific parts of that element. For example, the “::after” keyword adds some text value after the selected element and can apply a specific styling only to this text.


2 Answers

String script = "return window.getComputedStyle(document.querySelector('.analyzer_search_inner.tooltipstered'),':after').getPropertyValue('content')";
                        Thread.sleep(3000);
                        JavascriptExecutor js = (JavascriptExecutor) driver;
                        String content = (String) js.executeScript(script);
                        System.out.println(content);
like image 174
Nitin Singh Avatar answered Sep 19 '22 02:09

Nitin Singh


I bet also to try with the javascript solution above.

Since ::after & ::before are a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML). While the end result is not actually in the DOM, it appears on the page as if it is - you see it but can't really locate it with xpath for example (https://css-tricks.com/almanac/selectors/a/after-and-before/).

I can also suggest if possible to have different IDs or if they in different place in the DOM make more complex xpath using above/below elements and see if it is displayed.

like image 37
Rain9333 Avatar answered Sep 22 '22 02:09

Rain9333