HOWTO · Python
How to Find Element by Text Using Selenium in Python
This tutorial demonstrates how to find elements by text using Selenium in Python. Learn various methods including XPath, CSS selectors, and link text techniques to enhance your web automation skills. Perfect for both beginners and experienced developers, this guide provides practical examples and clear explanations to help you navigate complex web pages efficiently.
On this page
Selenium’s By.XPATH locator is the general choice for finding a page element by visible text. Use link-text locators for anchors; CSS selectors do not match text on their own, so combine a CSS selection with a text check when needed. The locator snippets below use Selenium 4 APIs. Browser examples require Selenium and a configured browser driver; the standalone Python example demonstrates exact text matching without those external dependencies.
Method 1: Using XPath to Find Element by Text
Use //*[text()='About Us'] when the target is a direct text node. For text that may contain extra whitespace or nested elements, //*[normalize-space(.)='About Us'] compares the element’s combined descendant text after trimming and collapsing whitespace. Narrow the tag or attributes when several elements may contain the same text.
Run this standalone text-matching example:
from html.parser import HTMLParser
class LinkTextParser(HTMLParser):
def __init__(self):
super().__init__()
self.in_link = False
self.links = []
def handle_starttag(self, tag, attrs):
if tag == "a":
self.in_link = True
self.links.append("")
def handle_endtag(self, tag):
if tag == "a":
self.in_link = False
def handle_data(self, data):
if self.in_link:
self.links[-1] += data
html = '<a href="#about">About Us</a><a href="#help">Help</a>'
parser = LinkTextParser()
parser.feed(html)
matches = [link.strip() for link in parser.links if link.strip() == "About Us"]
print(matches[0])
About Us
This example shows the text comparison on a fixed HTML string; it does not drive a browser. In Selenium, wait for the target when the page renders it asynchronously, for example with WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[normalize-space(.)='About Us']"))). Close the driver in a finally block so cleanup also happens if a lookup fails.
Method 2: Using CSS Selectors to Find Element by Text
CSS has no standard visible-text selector. First select a small, relevant set of candidates, then compare their text in the browser. For example, select buttons with document.querySelectorAll('button.action') and find the one whose textContent.trim() equals the requested label. A broad query can match both a container and its child, and textContent includes hidden descendant text, so use a specific selector and verify visibility when those distinctions matter. Selenium can run this check with driver.execute_script(...); wait for the relevant container before querying dynamic content.
Method 3: Using Link Text and Partial Link Text
For anchor elements, Selenium provides By.LINK_TEXT and By.PARTIAL_LINK_TEXT. For example, driver.find_element(By.LINK_TEXT, "Python documentation") finds an exact link label, while driver.find_element(By.PARTIAL_LINK_TEXT, "Python guide") finds a link containing that phrase. These locators do not apply to buttons, headings, or other element types. Use an explicit wait if the link is added after the initial page load, and prefer exact text when links have similar labels.
Conclusion
Choose XPath for general text matching, especially when whitespace or element relationships matter. CSS can narrow the candidates, but a separate text comparison is required. Link-text locators are concise for anchors. If a lookup does not find the expected element, confirm its text and type, check whether multiple elements match, and wait for client-rendered content instead of relying on a fixed delay.
FAQ
-
How do I install Selenium for Python? Install the Selenium package with
python -m pip install selenium. Python includes itstimemodule; it is not a package to install. Browser control also needs a supported browser and driver setup. -
Which browsers can Selenium control? Selenium can control common browsers such as Chrome, Firefox, Safari, and Edge through their browser drivers. Availability depends on your operating system and installed browser.
-
Can Selenium run headlessly? Yes. Set the browser’s headless option in its driver options before creating the driver. Headless mode still needs the browser and driver setup.
-
When should I use XPath or CSS? XPath can match text and navigate relationships in the DOM. CSS selects by element, class, and attributes, but has no standard text matching; add a separate text check if needed.
-
Can Selenium test single-page applications? Yes. Wait for the expected element or state after the application updates. A successful navigation does not guarantee that client-rendered content is ready.