If you want to locate the element that matches your criteria, you can use the following code:
sixth_item = driver.find_element_by_id("coption5")
To find this element using only the coption identifier, you have a few options with different Locator Strategies:
Using XPATH
and starts-with()
:
sixth_item = driver.find_element_by_xpath("//*[starts-with(@id, 'coption')]")
Using XPATH
and contains()
:
sixth_item = driver.find_element_by_xpath("//*[contains(@id, 'coption')]")
Using CSS_SELECTOR
and ^
(wildcard of starts-with):
sixth_item = driver.find_element_by_css_selector("[id^='coption']")
Using CSS_SELECTOR
and *
(wildcard of contains):
sixth_item = driver.find_element_by_css_selector("[id*='coption']")
For more information
You can read further discussions on dynamic CssSelectors in the following links:
- How to get selectors with dynamic part inside using Selenium with Python?
- Java Selenium webdriver expression finding dynamic element by ccs that starts with and ends with
- How to click a dynamic link within a Drupal 8 website using xpath/css selector while automating through Selenium and Python
- Finding elements by CSS selector with ChromeDriver (Selenium) in Python