How to find elements by XPath in Selenium

XPath selectors are one of the most popular ways to parse HTML pages when web scraping. Using Selenium, to find elements by XPath we can use driver.find_element() and driver.find_elements() methods:

from selenium import webdriver
from import By

driver = webdriver.Chrome()

element = driver.find_element(By.XPATH, '//p')
# then we can get the element text
"Availing himself of the mild, summer-cool weather that now reigned in these latitudes..."
# we can also get tag name and attributes:

# for multiple elements we need to iterate
for element in driver.find_elements(By.XPATH, '//p'):


Also see: How to find elements by CSS selector in Selenium

Provided by Scrapfly

This knowledgebase is provided by Scrapfly — a web scraping API that allows you to scrape any website without getting blocked and implements a dozens of other web scraping conveniences. Check us out 👇