16. Locating WebElements: By Strategies, CSS & Robust XPath

Locator Strategies via By

from selenium.webdriver.common.by import By

btn_id = (By.ID, "login-btn")
btn_css = (By.CSS_SELECTOR, "button[data-testid='submit']")
btn_xpath = (By.XPATH, "//button[contains(text(), 'Submit')]")

# XPath Axes:
user_delete_btn = (By.XPATH, "//tr[td[text()='Dhanu']]//button[@aria-label='Delete']")

Task

  • Implement make_button_locator(button_text):
  • 1. Return (By.XPATH, f"//button[contains(normalize-space(), '{button_text}')]").
πŸ’‘Common doubts
Did you like the problem?
Lesson 16 of 43
Python
Loading code editor…
Next