1

This is the code for my button in HTML:

<button type="submit" class="btn btn-secondary" id="single_button618eda460b64617" title="">Re-attempt quiz</button>

and the class I define for many other component and the id always begin with single_button.

but the end is random character. And it's the only one that starts with this prefix.

How can i use button=driver.find_element_by_id(), but for the first single_button?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

1

As the element is the only where the prefix of the id attribute starts with this unique value you can use either of the following dynamic Locator Strategies:

  • Using css_selector:

    button = driver.find_element(By.CSS_SELECTOR, "button[id^='single_button']")
    

PS: id^ denotes, value of id attribute starts with.

  • Using xpath:

    button = driver.find_element(By.XPATH, "//button[starts-with(@id, 'single_button')]")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352