0
0
Selenium-pythonHow-ToBeginner · 3 min read

How to Find Element by Text in Selenium: Simple Guide

To find an element by its visible text in Selenium, use driver.find_element(By.XPATH, "//*[text()='your text']") or driver.find_element(By.XPATH, "//tagname[contains(text(),'partial text')]"). XPath is the most common and reliable way to locate elements by exact or partial text.
📐

Syntax

Use the find_element method with By.XPATH to locate elements by text. The XPath expression //*[text()='exact text'] finds elements with exact visible text. Use contains(text(),'partial text') for partial matches.

  • driver.find_element(By.XPATH, "//*[text()='text']"): Finds element with exact text.
  • driver.find_element(By.XPATH, "//tagname[contains(text(),'text')]"): Finds element of specific tag containing text.
python
element = driver.find_element(By.XPATH, "//*[text()='Example Text']")
💻

Example

This example opens a simple webpage and finds a button by its visible text using XPath. It then prints the button's text to confirm the correct element was found.

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 in headless mode

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

# Open a simple HTML page with a button
html = '''
<html>
  <body>
    <button>Click Me</button>
  </body>
</html>
'''

# Load HTML content using data URL
driver.get("data:text/html;charset=utf-8," + html)

# Find button by exact text
button = driver.find_element(By.XPATH, "//button[text()='Click Me']")
print(button.text)  # Output: Click Me

driver.quit()
Output
Click Me
⚠️

Common Pitfalls

  • Using By.LINK_TEXT only works for <a> tags, not other elements.
  • Exact text match fails if extra spaces or hidden characters exist; use contains() for partial matching.
  • Case sensitivity matters in XPath text matching.
  • Using CSS selectors cannot directly find elements by text; XPath is preferred.
python
from selenium.webdriver.common.by import By

# Wrong: Using LINK_TEXT for a button (not a link)
# button = driver.find_element(By.LINK_TEXT, 'Click Me')  # This will fail

# Right: Use XPath for any element by text
button = driver.find_element(By.XPATH, "//button[text()='Click Me']")
📊

Quick Reference

MethodUsageNotes
Exact text match//*[text()='text']Finds element with exact visible text
Partial text match//*[contains(text(),'partial')]Finds element containing partial text
Specific tag + text//tagname[text()='text']Finds element of tag with exact text
Link text (only <a>)By.LINK_TEXTOnly for anchor tags
MethodUsageNotes
Exact text match//*[text()='text']Finds element with exact visible text
Partial text match//*[contains(text(),'partial')]Finds element containing partial text
Specific tag + text//tagname[text()='text']Finds element of tag with exact text
Link text (only )By.LINK_TEXTOnly for anchor tags

Key Takeaways

Use XPath with text() to find elements by visible text in Selenium.
For partial matches, use contains(text(),'partial text') in XPath.
By.LINK_TEXT works only for anchor () elements, not others.