Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click on pseudo ::before or ::after element using Selenium, Python Edition

This Java Version of this question is both aptly asked, and answered, at Java Version.

However, the question, and therefore the excellent accepted answer, was specific to Java.

How can this be done with Python? hence asking this question and providing details of my specific case below.

I am trying to click the select all checkbox element (well, inside the checkbox really) as shown in the below picture:

enter image description here

The actual clickable area is sitting inside the checkbox element, represented by the ::before pseudo element in the DOM shown in picture above.

Noting that the pseudo elements cannot be referenced using XPATH, I want to access this ::before pseudo element by physically using the mouse click, referencing the ::before element by pointing the mouse to the center of the checkbox at the below XPATH, using Selenium Python:

//span[text()="Transmission (TX) to eNodeB"]/ancestor::h2/following-sibling::div/descendant::label[1]

The XPATH is working to locate the top left of the checkbox element, validated by the 1 result yellow highlighted using chrome's find element in picture.

like image 242
TendaiM Avatar asked Feb 19 '26 23:02

TendaiM


1 Answers

There is no way you can select or click CSS pseudo-element since even if you see it in HTML DOM-tree it's not present in DOM!

You need to select checkbox input field. Try XPath

//input[@type='checkbox']

or more specific

//div[.//span='Transmission (TX) to eNodeB']//input[@type='checkbox']

P.S. To get better solution you need to provide HTML as text for ancestors as well

like image 59
DonnyFlaw Avatar answered Feb 21 '26 12:02

DonnyFlaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!