Best practices

  • Ensure that the web page is fully loaded before attempting to retrieve text to avoid `NoSuchElementException`.

  • Use explicit waits to handle elements that might take longer to appear due to dynamic content loading.

  • Always validate the correctness of the locator (ID, class name, XPath) to ensure it uniquely identifies the target element.

  • Keep your Selenium drivers updated to ensure compatibility with the latest versions of web browsers.

Scrollable code block. Use arrow keys to scroll.

Common issues

  • Ensure that the element is visible on the page before attempting to retrieve its text, as hidden elements might return an empty string.

  • Check for iframes and switch to the appropriate iframe context if the element is within one, as Selenium does not automatically manage iframe switches.

  • Handle potential `StaleElementReferenceException` by re-locating the element or using a retry mechanism in your script.

  • Consider using `get_attribute('textContent')` instead of `.text` if you need to retrieve text that includes child element texts.

Scrollable code block. Use arrow keys to scroll.

Try Oxylabs' Proxies & Scraper API

Residential Proxies

Self-Service

Human-like scraping without IP blocking

From

8

Datacenter Proxies

Self-Service

Fast and reliable proxies for cost-efficient scraping

From

1.2

Web scraper API

Self-Service

Public data delivery from a majority of websites

From

49

Useful resources

Web Scraping with Selenium: Step-by-Step Tutorial
Gabija Fatenaite avatar

Gabija Fatenaite

2026-02-09

Puppeteer vs Selenium: Which to Choose
author avatar

Yelyzaveta Hayrapetyan

2025-05-27

How to Find Elements With Selenium in Python
Enrika avatar

Enrika Pavlovskytė

2024-06-21

Get the latest news from data gathering world