Click a Button by Text Using Python and Selenium

I’ve lost count of how many UI tests fail because a button moved, a class name changed, or the markup got “cleaned up” by a front-end refactor. Yet the button’s visible text usually stays stable, because product teams care about the words a user sees. If you can click by text, you can make tests that survive most layout tweaks. That’s why I keep a text-first strategy in my Selenium toolkit, even in 2026 when we have richer selectors and AI-assisted test generation.

You should expect a few tradeoffs: text can be localized, A/B tested, or nested inside spans, and Selenium’s older convenience methods are now deprecated. Still, a solid text-based click flow is one of the quickest ways to get reliable coverage for critical paths like Sign In and Checkout. I’ll walk you through how I do it today with Python and Selenium 4, how to avoid the traps that cause flaky clicks, and how to decide when text is the right selector and when it isn’t. You’ll see a fully runnable script, modern waiting patterns, and practical fallback selectors for real-world pages.

Why text-based clicks still matter

I treat text as the closest thing to a stable contract between design and automation. Class names are a styling detail; text is a product decision. If the words on a button change, you want the test to break, because the user-facing behavior changed. That makes text an honest selector.

Text-based clicks also help when you’re dealing with templated UI components that generate unpredictable IDs. If a component library gives you random suffixes like button-7843, you’re stuck without a stable attribute. Text gives you a clean handle that matches the user’s mental model.

That said, text is not always the safest choice. If the UI is localized, you must handle multiple languages. If the app uses an icon button with no visible text, you need an alternate selector. I think of text selectors as a first option for visible, user-facing buttons, and a fallback to attribute or ARIA selectors when the UI is hidden, localized, or dynamic.

Here’s the rule I follow: if a user can read it and a product manager might change it, it’s a good test trigger. If a user cannot read it, text is fragile. That mindset keeps me from overusing text where it doesn’t belong.

Selenium 4 setup and what changed since older guides

If you learned Selenium years ago, you might remember methods like findelementbylinktext. In Selenium 4, those convenience methods are deprecated. You now use the unified find_element with a By strategy, which is more consistent and easier to read once you get used to it.

You should install Selenium with pip:

pip3 install selenium

In 2026, Selenium Manager is built in and can download browser drivers for you automatically. I still pin drivers in CI for determinism, but locally I let Selenium Manager handle it because it saves time. If you prefer manual installs, download the driver that matches your browser version and put it on PATH, or pass the driver path explicitly.

Here’s the modern pattern for link text in Selenium 4:

from selenium.webdriver.common.by import By

element = driver.findelement(By.LINKTEXT, "Sign In")

If the button is not a link but a real

Scroll to Top