How to Find Elements in Selenium Python: Syntax and Examples
In Selenium Python, you find elements using
find_element or find_elements methods with locators like By.ID, By.NAME, or By.CSS_SELECTOR. These methods return a single element or a list of elements matching the locator on the web page.Syntax
Use driver.find_element(By.LOCATOR_TYPE, 'locator_value') to find a single element, or driver.find_elements(By.LOCATOR_TYPE, 'locator_value') to find multiple elements. By is a class that provides locator types like ID, NAME, CSS_SELECTOR, XPATH, etc.
Example locator types:
ID: Finds element by its unique id attribute.NAME: Finds element by its name attribute.CSS_SELECTOR: Finds element using CSS selectors.XPATH: Finds element using XPath expressions.
python
from selenium import webdriver from selenium.webdriver.common.by import By # Initialize driver (example with Chrome) driver = webdriver.Chrome() # Find a single element by ID element = driver.find_element(By.ID, 'element_id') # Find multiple elements by class name elements = driver.find_elements(By.CLASS_NAME, 'class_name')
Example
This example opens a webpage, finds the search input box by its name attribute, types a query, and finds all links on the page by their tag name.
python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time # Setup Chrome driver driver = webdriver.Chrome() # Open Google homepage driver.get('https://www.google.com') # Find search box by name and type 'Selenium' search_box = driver.find_element(By.NAME, 'q') search_box.send_keys('Selenium') search_box.send_keys(Keys.RETURN) # Wait for results to load time.sleep(2) # Find all links on the results page links = driver.find_elements(By.TAG_NAME, 'a') print(f'Found {len(links)} links on the page.') # Close the browser driver.quit()
Output
Found 50 links on the page.
Common Pitfalls
Common mistakes when finding elements include:
- Using
find_elementwhen multiple elements match, causing errors if more than one exists. - Not importing
Byfromselenium.webdriver.common.by. - Using deprecated locator methods like
find_element_by_idwhich are removed in recent Selenium versions. - Not waiting for elements to load before finding them, causing
NoSuchElementException.
Always use explicit waits to ensure elements are present before interacting.
python
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wrong way (may fail if element not ready) element = driver.find_element(By.ID, 'my_id') # Right way with explicit wait wait = WebDriverWait(driver, 10) element = wait.until(EC.presence_of_element_located((By.ID, 'my_id')))
Quick Reference
| Locator Type | Description | Example Usage |
|---|---|---|
| By.ID | Find element by id attribute | driver.find_element(By.ID, 'username') |
| By.NAME | Find element by name attribute | driver.find_element(By.NAME, 'password') |
| By.CLASS_NAME | Find elements by class attribute | driver.find_elements(By.CLASS_NAME, 'btn') |
| By.CSS_SELECTOR | Find element using CSS selector | driver.find_element(By.CSS_SELECTOR, 'div.content > p') |
| By.XPATH | Find element using XPath expression | driver.find_element(By.XPATH, '//input[@type="submit"]') |
| By.TAG_NAME | Find elements by tag name | driver.find_elements(By.TAG_NAME, 'a') |
| By.LINK_TEXT | Find link by exact text | driver.find_element(By.LINK_TEXT, 'Home') |
| By.PARTIAL_LINK_TEXT | Find link by partial text | driver.find_element(By.PARTIAL_LINK_TEXT, 'Read more') |
Key Takeaways
Use driver.find_element and driver.find_elements with By locators to find elements in Selenium Python.
Always import By from selenium.webdriver.common.by for locator strategies.
Prefer explicit waits to avoid errors when elements are not immediately available.
Avoid deprecated locator methods; use the current By-based syntax.
Use the correct locator type based on the element attribute or structure for reliable tests.