Skip to main content

Best practices

  • Ensure that the ID used is unique within the HTML document to avoid selecting the wrong element.

  • Use By.ID instead of deprecated alternatives like find_element_by_id for cleaner, future-proof code.

  • Always verify that the element is present and visible on the page before attempting to interact with it to prevent runtime errors.

  • Use explicit waits (WebDriverWait) when working with elements that may load asynchronously.

Scrollable code block. Use arrow keys to scroll.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Initialize WebDriver
driver = webdriver.Chrome()
# Navigate to the page
driver.get("https://sandbox.oxylabs.io/products")
# Method 1: Find element by ID using By.ID (recommended)
element = driver.find_element(By.ID, "product-1")
print(element.text)
# Method 2: Use WebDriverWait for elements that load dynamically
wait_element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "dynamic-id"))
)
print(wait_element.text)
# Close the browser
driver.quit()

Common issues

  • Make sure the ID attribute in the HTML is correctly spelled and matches exactly with the ID provided in your Selenium script.

  • Update your Selenium WebDriver regularly to ensure compatibility with the latest browser versions, which can affect how elements are identified.

  • Check for changes in the web page structure during development, as dynamic content might alter element IDs.

  • Avoid using deprecated methods like `find_element_by_id` and stick to `find_element(By.ID, "id")` to future-proof your code.

Scrollable code block. Use arrow keys to scroll.
# Incorrect ID spelling
element = driver.find_element(By.ID, "product1") # Might cause NoSuchElementException

# Correct ID spelling
element = driver.find_element(By.ID, "product-1") # Correct usage

# Using an outdated WebDriver
driver = webdriver.Chrome() # May fail if Chrome has been updated to a new version not supported by the driver

# Using deprecated method
element = driver.find_element_by_id("product-1") # Deprecated, might not work in future Selenium versions

# Correct method usage
element = driver.find_element(By.ID, "product-1") # Recommended method, more likely to be supported in future updates

# Dynamic content changing ID
element = driver.find_element(By.ID, "temp-id") # Fails if ID changes after page updates

# Checking element after ensuring page has loaded dynamic content
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "dynamic-id")))
element = driver.find_element(By.ID, "dynamic-id") # More reliable for dynamic content

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

BeautifulSoup Tutorial - How to Parse Web Data With Python
BeautifulSoup Tutorial - How to Parse Web Data With Python
Authors avatar

Adomas Sulcas

2026-10-05

Web Scraping with Selenium: Step-by-Step Tutorial
Gabija Fatėnaitė

Gabija Fatėnaitė

2026-02-09

How to Find Elements With Selenium in Python
How to Find Elements With Selenium in Python
Enrika avatar

Enrika Pavlovskytė

2024-06-21

Get the latest news from data gathering world

Scale up your business with Oxylabs®