0

I have a list of labels and want to scrap only some of them.

<div>
  <label class="search-reusables__value-label">
    ::before
    <p class="display-flex">...</p>
  </label>

  <label class="search-reusables__value-label">
    ::before
    <p class="display-flex">...</p>
    ::after
  </label>

  <label class="search-reusables__value-label">
    ::before
    <p class="display-flex">...</p>
  </label>

  <label class="search-reusables__value-label">
    ::before
    <p class="display-flex">...</p>
    ::after
  </label>
</div>

I'm only interested in the labels that have the ::after pseudo-element.

One label I want to get for example:

<label class="search-reusables__value-label">
  ::before
  <p class="display-flex">...</p>
  ::after
</label>

And one I'm not interested in:

<label class="search-reusables__value-label">
  ::before
  <p class="display-flex">...</p>
</label>

This question seems close to what I want to do: How locate the pseudo-element ::before using Selenium Python, but I couldn't get it to work.

I tried for example

script = "return window.getComputedStyle(document.querySelector('label.search-reusables__value-label'),':after').getPropertyValue('content')"
print(driver.execute_script(script).strip())

But the result I get is (a blank space).

Any idea?

Thanks for your help.

victorfink
  • 343
  • 4
  • 17

1 Answers1

0

In the similar question you mentioned, there is a text inside the ::after pseudo element and the goal there is to extract that text.
While in your case there is no text inside the after pseudo element and you want to locate the parent elements themselves based on existence of pseudo ::after elements inside them.
This can be done with the following JavaScript:

driver.execute_script("document.querySelector('label.search-reusables__value-label'), ':after'")

While the similar question here is this one

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Thanks a lot for your help! But I don't know how to make this work in the example I gave. I executed `driver.execute_script("document.querySelector('label.search-reusables__value-label'), ':after'")` but it returns nothing? How can I get the 2 labels with the code you provided? – victorfink Mar 22 '22 at 08:28
  • Using `driver.execute_script("return document.querySelectorAll('label.search-reusables__value-label')")` I can get all the labels as webelements, but whenever I add the `':after'` part it returns `:after` and not the webelements. Any idea? – victorfink Mar 22 '22 at 08:51
  • Currently I have no solution for that, I'm sorry. Maybe I will try doin that later. In case you find solution yourself - please let me know, that is interesting. – Prophet Mar 22 '22 at 10:54
  • Oh ok, I didn't find anything else for the moment unfortunately. – victorfink Mar 22 '22 at 11:49
  • Me too, also currently I have no time for that, I'm sorry. But in case you finally find a solution - and I believe you will do - please let me know, it's interesting. – Prophet Mar 22 '22 at 11:52
  • 1
    I found another solution (using `is_selected` with a webelement) which works in my use case. But I don't have the time either to look more into this, so if anyone has a solution, I will gladly accept it. – victorfink Mar 22 '22 at 14:17