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
Asked
Active
Viewed 3.4k times
11

BoltClock
- 700,868
- 160
- 1,392
- 1,356

Jeremy Borg
- 413
- 2
- 6
- 16
-
1The elements dont have an id. Why dont you just select it using classname? – LittlePanda Apr 21 '15 at 12:44
-
because both elements have the same classname – Jeremy Borg Apr 21 '15 at 13:27
-
1How are you differentiating between them in CSS, in order to apply the `:before` pseudo element to the correct one? – Shaggy Apr 21 '15 at 15:52
-
See this answer - http://stackoverflow.com/a/28265738/4720017 – LittlePanda Apr 22 '15 at 08:23
-
Possible duplicate of [Selenium WebDriver get text from CSS property "content" on a ::before pseudo element](https://stackoverflow.com/questions/28244911/selenium-webdriver-get-text-from-css-property-content-on-a-before-pseudo-ele) – SiKing Jun 18 '19 at 18:42
-
I think it would make more sense to filter them based on which is visible or clickable, if such a method exists in Selenium-webdriver. It exists in WDIO which is (javascript) webdriver-related. – Carolus Dec 09 '19 at 13:13
2 Answers
4
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.

Rain9333
- 570
- 4
- 22
1
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);

Nitin Singh
- 31
- 1