Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to include regex in the css selector in python selenium

I am trying the extract the text by using the CSS selector. as variables are dynamic in nature they are continuously updating.

#profileCard-ACoAABI4A30BKLei-DtaS6B6vQo-3ejw-nzI1W8-EDUCATION-en-US 

this is a CSS selector

driver.find_element_by_css_selector('#profileCard-ACoAABI4A30BKLei-DtaS6B6vQo-3ejw-nzI1W8-EDUCATION-en-US').text

x_path = //*[@id="profileCard-ACoAABI4A30BKLei-DtaS6B6vQo-3ejw-nzI1W8-EDUCATION-en-US"]
# here i tried to apply the regex
driver.find_element_by_xpath("//*[ends-with(@id,'EDUCATION-en-US')]") 

But I’m getting an error as ‘InvalidSelectorException’.
Is there any way to get the data without error?

‘ACoAABI4A30BKLei-DtaS6B6vQo-3ejw-nzI1W8’ this path will be continuously updating.
‘#profileCard’, ‘EDUCATION-en-US’ there will be no change in this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help or lead to solve the issue would be very helpful. Thanks in advance

>Solution :

This should do what you are looking for:

driver.find_element_by_css_selector('[id^="profileCard-"][id$="-EDUCATION-en-US"]').text

The ^ denotes what the id should begin with.

The $ denotes what the id should end with.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading