You should use class and/or id to make shorter xpath.

When you find cards then you can use every card with xpath which starts with ./ - so it will be xpath relative to this element and it will search only inside this element.

You can also use // in any part of xpath to skip some tags which are not important.

You can use other find_element_by_ and find_elements_by_ with card and it will also search only inside this element - so it will be relative.

import selenium.webdriver

driver = selenium.webdriver.Chrome() # Firefox()

driver.get('https://travel.padi.com/s/liveaboards/caribbean/')

all_cards = driver.find_elements_by_xpath('//div[@class="boat search-page-item-card "]')

for card in all_cards:
    title = card.find_element_by_xpath('.//a[@class="shop-title"]/span')
    desc  = card.find_element_by_xpath('.//p[@class="shop-desc-text"]')
    price = card.find_element_by_xpath('.//p[@class="cur-price"]/strong/span')

    print('title:', title.text)
    print('desc:',  desc.text)
    print('price:', price.text)

    all_dates = card.find_elements_by_css_selector('.cell.date')

    for date in all_dates:
        day, month = date.find_elements_by_tag_name('span')
        print('date:', day.text, month.text)

    print('---')

Example result (you can have price in different currency)

title: CARIBBEAN EXPLORER II
desc: With incredible, off-the-beaten path itineraries that take guests to St Kitts, Saba and St Maarten, this leading liveaboard spoils divers with five dives each day, scenic geography and a unique slice of Caribbean culture.
Dates do not match your search criteria
price: PLN 824
date: 7 DEC
date: 14 DEC
date: 21 DEC
date: 28 DEC
---
title: BAHAMAS AGGRESSOR
desc: Featuring five dives a day, the well-regarded Bahamas Aggressor liveaboard is the ideal choice for divers who want to spend as much time under the water as possible then relax in an onboard Jacuzzi.
Dates do not match your search criteria
price: PLN 998
date: 7 DEC
date: 14 DEC
date: 21 DEC
date: 28 DEC
---
Answer from furas on Stack Overflow
๐ŸŒ
Selenium
selenium.dev โ€บ documentation โ€บ webdriver โ€บ elements โ€บ finders
Finding web elements | Selenium
September 6, 2025 - from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://www.example.com") ##get elements from parent element using TAG_NAME # Get element with tag name 'div' element = driver.find_element(By.TAG_NAME, 'div') # Get all the elements available with tag name 'p' elements = element.find_elements(By.TAG_NAME, 'p') for e in elements: print(e.text) ##get elements from parent element using XPATH ##NOTE: in order to utilize XPATH from current element, you must add "." to beginning of path # Get first element of tag 'ul' element = driver.find_element(By.XPATH, '//ul') # get children of tag 'ul' with tag 'li' elements = element.find_elements(By.XPATH, './/li') for e in elements: print(e.text)
๐ŸŒ
Bird Eats Bug
birdeatsbug.com โ€บ blog โ€บ contains-text-in-selenium
Using XPath Contains Text in Selenium with Practical Examples | Bug Tracking Blog @ Bird Eats Bug
Discover how to locate elements by exact or partial text in Selenium using XPath text() and contains with clear, real-world examples.
Discussions

selenium in python finding an element via relative xpath - Stack Overflow
I am trying to navigate the PADI liveaboard page to scrape some boat, departure date and price info. I was able to get the xpath from chrome debug console and have selenium find it. But i want to m... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Unable to find element by xpath using selenium - Stack Overflow
I'm very new to Python and I'm trying to follow along with a video on web scraping with Selenium. In the video, the person walking through how to do this has a whole bunch of different possible met... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Can't get element by XPATH
You're using an absolute xpath, which is almost always a bad idea. This xpath might work at one point, but it's also very prone to failure with even the slightest change in the page's DOM structure. Try coming up with an xpath that more uniquely identifies the element instead of tracing it all the way back to the html tag. More on reddit.com
๐ŸŒ r/selenium
4
2
September 7, 2021
Using xpath to find element does not always work
While what has been said is correct, IDs and Name selectors are preferred, XPath can be easy to use if used correctly. I like using https://devhints.io/xpath as a guide, but you can actually build some fairly robust locators using xpath. For instance, a lot of buttons on a site I work with don't have ID or Name attributes. I've made some xpath locators like //input[@type="submit" and text() = "Save"] When using something like devtools, just right clicking an element and extracting the Xpath is going to give you a horribly long and hideous xpath locator - those are almost always going to break and are extremely brittle. You can use those for a quick proof of concept but for robust locators, that's generally what you want to avoid, but don't avoid xpath all together, because it can certainly make good locators. More on reddit.com
๐ŸŒ r/selenium
12
1
May 12, 2021
People also ask

What is XPath?
XPath is XSLT standard element. It can be used to navigate elements in XML or HTML DOM contents.
๐ŸŒ
testsigma.com
testsigma.com โ€บ testsigma home โ€บ blog โ€บ find element by xpath: a complete guide
Find Element by XPath: A Complete Guide
How to find the XPath by Text?
The XPath can also be constructed to find text in the DOM element. In such cases, you need to use the text() functions in the XPath.Example://ul/li[text()='banana'].
๐ŸŒ
testsigma.com
testsigma.com โ€บ testsigma home โ€บ blog โ€บ find element by xpath: a complete guide
Find Element by XPath: A Complete Guide
How to find the XPath Value?
Any modern browserโ€™s developer tools can help to find the XPath value; there are third-party plugins that can help you to find the XPath easily. However, the browser dev tools can help you in most of the scenarios.
๐ŸŒ
testsigma.com
testsigma.com โ€บ testsigma home โ€บ blog โ€บ find element by xpath: a complete guide
Find Element by XPath: A Complete Guide
๐ŸŒ
Selenium Python
selenium-python.readthedocs.io โ€บ locating-elements.html
4. Locating Elements โ€” Selenium Python Bindings 2 documentation
from selenium.webdriver.common.by import By driver.find_element(By.XPATH, '//button[text()="Some text"]') driver.find_elements(By.XPATH, '//button')
๐ŸŒ
BrowserStack
browserstack.com โ€บ home โ€บ guide โ€บ find element by xpath in selenium in 2026
How to find element by XPath in Selenium [2026] | BrowserStack
December 19, 2025 - XPath in Selenium is a locator strategy used to navigate the HTML structure of a webpage and find elements based on their hierarchy, attributes, or text.
๐ŸŒ
Selenium
selenium.dev โ€บ documentation โ€บ webdriver โ€บ elements โ€บ locators
Locator strategies | Selenium
February 16, 2026 - import pytest from selenium import webdriver from selenium.webdriver.common.by import By def test_class_name(): driver = webdriver.Chrome() driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html") element = driver.find_element(By.CLASS_NAME, "information") assert element is not None assert element.tag_name == "input" driver.quit() def test_css_selector(driver): driver = webdriver.Chrome() driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html") element = driver.find_element(By.CSS_SELECTOR, "#fname") assert element is not None assert element.get_
Find elsewhere
Top answer
1 of 2
7

You should use class and/or id to make shorter xpath.

When you find cards then you can use every card with xpath which starts with ./ - so it will be xpath relative to this element and it will search only inside this element.

You can also use // in any part of xpath to skip some tags which are not important.

You can use other find_element_by_ and find_elements_by_ with card and it will also search only inside this element - so it will be relative.

import selenium.webdriver

driver = selenium.webdriver.Chrome() # Firefox()

driver.get('https://travel.padi.com/s/liveaboards/caribbean/')

all_cards = driver.find_elements_by_xpath('//div[@class="boat search-page-item-card "]')

for card in all_cards:
    title = card.find_element_by_xpath('.//a[@class="shop-title"]/span')
    desc  = card.find_element_by_xpath('.//p[@class="shop-desc-text"]')
    price = card.find_element_by_xpath('.//p[@class="cur-price"]/strong/span')

    print('title:', title.text)
    print('desc:',  desc.text)
    print('price:', price.text)

    all_dates = card.find_elements_by_css_selector('.cell.date')

    for date in all_dates:
        day, month = date.find_elements_by_tag_name('span')
        print('date:', day.text, month.text)

    print('---')

Example result (you can have price in different currency)

title: CARIBBEAN EXPLORER II
desc: With incredible, off-the-beaten path itineraries that take guests to St Kitts, Saba and St Maarten, this leading liveaboard spoils divers with five dives each day, scenic geography and a unique slice of Caribbean culture.
Dates do not match your search criteria
price: PLN 824
date: 7 DEC
date: 14 DEC
date: 21 DEC
date: 28 DEC
---
title: BAHAMAS AGGRESSOR
desc: Featuring five dives a day, the well-regarded Bahamas Aggressor liveaboard is the ideal choice for divers who want to spend as much time under the water as possible then relax in an onboard Jacuzzi.
Dates do not match your search criteria
price: PLN 998
date: 7 DEC
date: 14 DEC
date: 21 DEC
date: 28 DEC
---
2 of 2
0

You need to use classes in items ./

I Simply Code For You You Can Try!

from selenium import webdriver
import pdb

browser = webdriver.Chrome()

browser.get('https://travel.padi.com/s/liveaboards/caribbean/')

items = browser.find_elements_by_xpath('//div[@class="boat-info"]')

for item in items :
    title = item.find_element_by_xpath('.//a[@class="shop-title"]/span')
    description = item.find_element_by_xpath('.//p[@class="shop-desc-text"]')
    price = item.find_element_by_xpath('.//p[@class="cur-price"]/strong/span')
    print('TITLE: ', title.text)
    print('DESCRIPTION: ', description.text)
    print('PRICE: ', price.text)
    print('------------------NEW-RECORD------------------------')
๐ŸŒ
Testsigma
testsigma.com โ€บ testsigma home โ€บ blog โ€บ find element by xpath: a complete guide
Find Element by XPath: A Complete Guide
July 31, 2025 - FindElements: A method in selenium, requires the LocatorStrategy as parameters. Returns the List containing the Web elements. ... Letโ€™s consider we have a list of footer links, and we wanted to fetch all the links at once and get the text. Then we can construct find elements by XPath like below,
๐ŸŒ
ScrapingBee
scrapingbee.com โ€บ webscraping-questions โ€บ selenium โ€บ how-to-find-elements-by-xpath-selenium
How to find elements by XPath in Selenium? | ScrapingBee
You can find elements by XPath selectors in Selenium by utilizing the find_element and find_elements methods and the By.XPATH argument. find_element returns the first occurence of the XPath selector being used, while find_elements returns all ...
๐ŸŒ
Headspin
headspin.io โ€บ home โ€บ blogs โ€บ how to use xpath in selenium - a complete guide for beginners
How to Use XPath in Selenium - A Complete Guide
July 26, 2023 - XPath locators in Selenium WebDriver are used to identify elements on a web page. These locators allow complex and flexible navigation of the web page's Document Object Model (DOM).
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ tags โ€บ google-chrome โ€บ new
'google-chrome' New Answers - Stack Overflow
As of Selenium 4.6, Selenium Manager was added and it automatically takes care of downloading and configuring the appropriate driver for you. So, you no longer need to use a DriverManager or specify ... ... The following Chrome CLI flag can be used to bypass the requirement that the site is cross-origin isolated when using the performance.measureUserAgentSpecificMemory API: --disable-web-security The ...
๐ŸŒ
Testim
testim.io โ€บ blog โ€บ find-element-by-xpath-selenium
How to Find an Element by XPath in Selenium - Testim Blog
October 14, 2022 - The most basic syntax for writing XPath in Selenium is as follows: ... Itโ€™s also possible to use the Contains() method in order to find a list of the elements that match the search predicate. This is particularly useful regarding elements whose values can change dynamically.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ software testing โ€บ best-way-to-get-element-by-xpath-using-javascript-in-selenium-webdriver
Best way to get element by XPath using JavaScript in Selenium WebDriver? - GeeksforGeeks
July 23, 2025 - Below code may be used to get the value for the id property of the search box element on the Google homepage: WebElement searchBox = driver.findElement(By.xpath("//input[@type=\"text\"]")); ... XPath is a useful tool for precisely and flexibly ...
๐ŸŒ
Reddit
reddit.com โ€บ r/selenium โ€บ can't get element by xpath
r/selenium on Reddit: Can't get element by XPATH
September 7, 2021 -

Hello guys,

I'm trying to interact with the interative menu of this page in order to automate several data downloads instead of making it by hand.

The thing is that when I copy an XPATH of a selector (for example when I try to get the XPATH of the "Commodities" menu), selenium says:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[9]/div[1]/div[3]/ul/li[6]"}

Does anyone know why I can't get the element?

Thank you all in advance!

EDIT WITH SOLUTION:

The problem was that items I want to find are inside an iframe. So I have to switch the context of the webdriver first. Code with the solution:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.dukascopy.com/swiss/english/marketwatch/historical/')

table = driver.find_element(By.XPATH, '/html/body/div/main/div[2]/div/div/div/p[3]/iframe')

driver.switch_to.frame(table)

driver.find_element(By.XPATH, '/html/body/div[9]/div[1]/div[3]/ul/li[13]').click()

๐ŸŒ
Fishtank
getfishtank.com โ€บ insights โ€บ findelement-and-findelements-by-xpath-in-selenium
FindElement And FindElements By XPath In Selenium Automation Testing | Fishtank
FindElements in Selenium command takes in By object as the parameter and returns a list of web elements. It returns an empty list if there are no elements found using the given locator strategy and locator value.
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ selenium โ€บ findelement by xpath in selenium
FindElement by XPath in Selenium
August 25, 2025 - There are multiple ways to uniquely identify a web element within the web page such as ID, Name, Class Name, Link Text, Partial Link Text, Tag Name and XPATH. WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue")); Selenium Find Element command takes in the By object as the parameter and returns an object of type list WebElement in Selenium.
๐ŸŒ
Bright Data
brightdata.com โ€บ faqs โ€บ selenium โ€บ find-elements-by-xpath
How to Find Elements by XPath in Selenium?
July 10, 2024 - Hereโ€™s an example of how to use ...xample.com") # Find an element by XPath and perform an action (e.g., click, get text) element = driver.find_element(By.XPATH, "//tagname[@attribute='value']") element.click() # Another example ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ find_element_by_xpath-driver-method-selenium-python
find_element(By.XPATH) driver method - Selenium Python - GeeksforGeeks
July 12, 2025 - Create a file called run.py to demonstrate the find_element(By.XPATH) method - ... # Python program to demonstrate # selenium # import webdriver from selenium import webdriver from selenium.webdriver.common.by import By # create webdriver object driver = webdriver.Firefox() # enter keyword to search keyword = "geeksforgeeks" # get geeksforgeeks.org driver.get("https://www.geeksforgeeks.org/") # get element element = driver.find_element(By.XPATH, "//form[input/@name ='search']") # print complete element print(element)
๐ŸŒ
Reddit
reddit.com โ€บ r/selenium โ€บ using xpath to find element does not always work
r/selenium on Reddit: Using xpath to find element does not always work
May 12, 2021 -

I am using python 3.7 in jupyter notebook. It works just as it should until it doesn't. It says that it cannot find the element and then it never can find it again. I have restarted kernel and that did not solve it. I am just using xpaths from the homepage of pyhton.org.

edit:

To get the xpath I am coping it from the inspect element in chrome. When I compare the error xpath and the one from the webpage they are an exact match.

๐ŸŒ
Udemy
udemy.com โ€บ development
Playwright JS/TS Automation Testing from Scratch & Framework
3 weeks ago - Locators supported by playwright and how to type into elements on page12:00 ยท Learn how Playwright locates elements with CSS selectors and XPath and builds selectors from id, class, and attributes; use fill and click with 5s assertion and 30s global timeouts.
Rating: 4.6 โ€‹ - โ€‹ 23.9K votes