2

I need to find an element by ID "start-ads", but the numbers at the end change every time. Is it possible to search for an element not by the whole ID, but only by its part?

Full element id: <div id="start-ads-202308">

2 Answers 2

1

To identify the following element:

<div id="start-ads-202308">

considering the static part of the value of id attribute you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "div[id^='start-ads-']")
    

    where ^ denotes starts-with

  • Using xpath:

    element = driver.find_element(By.XPATH, "//div[starts-with(@id, 'start-ads-')]")
    
Sign up to request clarification or add additional context in comments.

Comments

0

You can use find_element_by_css_selector and use a CSS selector that matches using a prefix

driver.find_element_by_css_selector("div[id^='start-ads-']")

Comments

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.