2

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

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

For value of id attributes like:

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

xpath doesn't supports ends-with(). Instead you can use either of the following options:

  • Using xpath:

    driver.find_element(By.XPATH, "//*[starts-with(@id, 'profileCard') and contains(@id, 'EDUCATION-en-US')]").text
    
  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "[id^='profileCard'][id$='EDUCATION-en-US']").text
    

4 Comments

Suppose if we have Xpath = //*[@id="ember265"]/div/div/div[1] like this where numbers are dynamic and this is XPath is independent of class and id. Then what is way to implement regex here
ember* indicates a dynamic element. You have to construct the locator considering that.
currenty i am using this. driver.find_element_by_xpath("//*[contains(@id,'ember')]/div/div/div[1]").text but it returning a empty list.
Let's discuss the issue in Selenium room.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.