How to Use XPath in Selenium Python: Syntax and Examples
In Selenium with Python, use
driver.find_element(By.XPATH, 'xpath_expression') to locate elements by XPath. XPath lets you find elements based on their position or attributes in the HTML structure.Syntax
The basic syntax to find an element using XPath in Selenium Python is:
driver.find_element(By.XPATH, 'xpath_expression')- Finds the first matching element.driver.find_elements(By.XPATH, 'xpath_expression')- Finds all matching elements as a list.By.XPATHis an import fromselenium.webdriver.common.bythat tells Selenium to use XPath.'xpath_expression'is the XPath string that describes the element location.
python
from selenium import webdriver from selenium.webdriver.common.by import By # Example syntax to find a single element element = driver.find_element(By.XPATH, "//tagname[@attribute='value']") # Example syntax to find multiple elements elements = driver.find_elements(By.XPATH, "//tagname")
Example
This example opens a webpage, finds a button by its XPath, clicks it, and prints the button text. It shows how to use XPath with Selenium in Python.
python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time # Setup Chrome driver (make sure chromedriver is in PATH) options = Options() options.add_argument('--headless') # Run browser in headless mode service = Service() driver = webdriver.Chrome(service=service, options=options) try: driver.get('https://www.w3schools.com/html/html_forms.asp') time.sleep(2) # Wait for page to load # Find the 'Submit' button by XPath submit_button = driver.find_element(By.XPATH, "//button[text()='Submit']") # Print button text print('Button text:', submit_button.text) # Click the button submit_button.click() print('Button clicked successfully') finally: driver.quit()
Output
Button text: Submit
Button clicked successfully
Common Pitfalls
- Incorrect XPath syntax: Missing quotes or wrong slashes can cause errors.
- Using absolute XPath: Absolute paths (starting with /html/body) are fragile and break easily if page changes.
- Not waiting for elements: Trying to find elements before they load causes
NoSuchElementException. - Using find_element instead of find_elements: If multiple elements match, use
find_elementsto get all.
Use relative XPath starting with // and include attributes or text to make locators stable.
python
from selenium.webdriver.common.by import By # Wrong: Absolute XPath (fragile) # element = driver.find_element(By.XPATH, "/html/body/div[2]/button") # Right: Relative XPath with attribute # element = driver.find_element(By.XPATH, "//button[@id='submitBtn']")
Quick Reference
Here are some common XPath patterns used in Selenium Python:
| XPath Pattern | Description | Example |
|---|---|---|
| //tagname | Selects all elements with this tag | //input |
| //tagname[@attr='value'] | Selects elements with attribute value | //button[@type='submit'] |
| //tagname[text()='text'] | Selects elements with exact text | //a[text()='Home'] |
| //tagname[contains(@attr, 'value')] | Selects elements where attribute contains value | //div[contains(@class, 'active')] |
| (//tagname)[index] | Selects element at position index (1-based) | (//input)[2] |
Key Takeaways
Use driver.find_element(By.XPATH, 'xpath') to locate elements by XPath in Selenium Python.
Prefer relative XPath with attributes or text for stable element location.
Avoid absolute XPath as it breaks easily with page changes.
Wait for elements to load before locating them to avoid errors.
Use find_elements(By.XPATH, 'xpath') to get multiple matching elements.