0
0
Selenium-pythonHow-ToBeginner · 4 min read

Find Element by Partial Text in Selenium: Simple Guide

To find an element by partial text in Selenium, use driver.find_element(By.XPATH, "//*[contains(text(), 'partial_text')]"). This XPath expression matches elements containing the specified partial text anywhere inside them.
📐

Syntax

The common way to find an element by partial text in Selenium is using XPath with the contains() function.

  • driver.find_element(By.XPATH, "//*[contains(text(), 'partial_text')]"): Finds the first element containing the partial text.
  • //*[contains(text(), 'partial_text')]: XPath expression selecting any element with text containing the given substring.
  • By.XPATH: Selenium locator strategy to use XPath.
python
element = driver.find_element(By.XPATH, "//*[contains(text(), 'partial_text')]")
💻

Example

This example shows how to open a webpage and find a button by partial text using Selenium with 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 options
options = Options()
options.add_argument('--headless')  # Run browser in headless mode

# Initialize driver (update path to chromedriver as needed)
service = Service('/path/to/chromedriver')
driver = webdriver.Chrome(service=service, options=options)

try:
    # Open example page
    driver.get('https://www.w3schools.com/howto/howto_css_buttons.asp')

    # Find button containing partial text 'Click'
    button = driver.find_element(By.XPATH, "//button[contains(text(), 'Click')]")

    # Print the full text of the found button
    print('Found button text:', button.text)

finally:
    driver.quit()
Output
Found button text: Click Me
⚠️

Common Pitfalls

Common mistakes when finding elements by partial text include:

  • Using text() when the text is inside child elements or has extra spaces, causing no match.
  • Not waiting for elements to load before searching, leading to NoSuchElementException.
  • Using case-sensitive text matching when the text case may vary.

To avoid these, ensure the text is exactly as expected, consider normalizing spaces, and use explicit waits.

python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wrong way: direct find without wait
# element = driver.find_element(By.XPATH, "//*[contains(text(), 'partial_text')]")

# Right way: wait until element is present
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'partial_text')]") ))
📊

Quick Reference

Summary tips for finding elements by partial text in Selenium:

  • Use XPath contains(text(), 'partial_text') for partial matching.
  • Use By.XPATH locator strategy.
  • Use explicit waits to handle dynamic content.
  • Check for case sensitivity and whitespace issues.
  • For elements with nested tags, consider .//*[contains(., 'partial_text')] to match text anywhere inside.

Key Takeaways

Use XPath with contains(text(), 'partial_text') to find elements by partial text.
Always use explicit waits to ensure elements are loaded before searching.
Check for text case and whitespace differences to avoid mismatches.
For nested text, use contains(., 'partial_text') to match text inside child elements.
Use By.XPATH locator strategy with Selenium's find_element method.