Use Selenium with Python to Target the XPath of a Particular Object
Tags: python, selenium, bigdata, webdriver
Published: 2023-12-19 15:06:13

---
title: Use Selenium with Python to Target the XPath of a Particular Object
published: true
date: 2023-12-18 00:00:00 UTC
tags: python,selenium,bigdata,webdriver
canonical_url: https://www.paigeniedringhaus.com/blog/use-selenium-with-python-to-target-the-x-path-of-a-particular-object
---

[![Webpage analytics dashboard](https://www.paigeniedringhaus.com/static/ff3ffa49ef2dcc688c0e993fbcde1f69/15ec7/dashboard-hero.jpg "Webpage analytics dashboard")](/static/ff3ffa49ef2dcc688c0e993fbcde1f69/0f98f/dashboard-hero.jpg)

## Introduction

Earlier this year, I needed to write a program for my friend to collect the data of one of the NFT collections on the [NFTrade](https://nftrade.com/) site. He wanted the following data included: all the NFTs currently for sale in the collection and the price of each NFT in US dollars based on the current market price of the BNB cryptocurrency that the NFT is for sale in, and he wanted it listed out line by line in a CSV file that he could sort and manipulate.

Unfortunately, the NFTrade website does not have a public API so instead of writing a Node.js script to fetch the data via HTTP calls, I built a small script to navigate to the website page and actually "scrape" the data off of it.

Having not written a web scraper before, I chose to write the program in Python as it seems to be a very popular programming language choice for a task such as this. While I built this scraper, the project requirements evolved and got more complex, and I learned a bunch of useful new techniques when using Python, which I'll be sharing in a series of posts over the coming months.

After I'd settled on using the Selenium Python package to start a Selenium WebDriver instance and scrape the data from NFTrade, I hit a snag: I was collecting all the individual NFT data to loop through and pull the details from, but each time the loop ran it only collected the data from the first NFT in the list.

I was stumped and turned to the Internet for help and (once again) Stack Overflow came through for me.

**When extracting scraped web data using Selenium WebDriver's XPath to target the data, in order to search inside a particular element instead of the whole document, a period (`.`) must be added in front of the XPath. I'll show you how to do it in this post.**

> **NOTE:** I am not normally a Python developer so my code examples may not be the most efficient or elegant Python code ever written, but they get the job done.

---

## Selenium Python package

For my project, I ended up using the [**Selenium Python package**](https://selenium-python.readthedocs.io/) because it can scrape websites with dynamically loaded data, which is how NFTrade works. A user lands on a collection of NFTs and as they scroll down the page, periodically more NFTs are loaded into the browser window via JavaScript.

Selenium Python operates the Selenium WebDriver software and drives a browser just like a user would, and although its original use was for automated end-to-end testing of software, it can also be used to scrape data off of live web pages.

If you'd like to read more about how to use Selenium Python for this, I encourage you to visit [my first blog post](https://www.paigeniedringhaus.com/blog/scrape-data-from-a-lazy-loading-website-with-selenium-python) on this subject where I go in depth on it.

### What is XPath?

Among the many useful methods included with the Selenium Python package are the methods [`find_element()`](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_element), [`find_elements()`](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_elements), and [`By.XPATH`](https://selenium-python.readthedocs.io/api.html#locate-elements-by).

The `find_element` methods do what their name implies: find an element (or elements) given a `By` strategy and locator.

[`By`](https://selenium-python.readthedocs.io/api.html#locate-elements-by) accepts element IDs, names, attributes, XPaths, class names, etc. And [XPath](https://www.w3schools.com/xml/xml_xpath.asp) is a syntax used to navigate through elements and attributes in a standard XML document (webpage).

Due to the NFTrade site's structure, I used XPath expressions to identify all the individual NFTs on the page and scrape the data from each NFT to include later in my CSV file.

### The Problem: XPath was targeting the whole document

After I'd written the code to initially fire up my Selenium WebDriver instance, navigate to the NFTrade site, and load the NFTs into the browser that I wanted to scrape the data from, I had a list of NFT info I needed to slim down to just the data points I wanted to include in the CSV.

> **NOTE:** If you'd like to see how to scrape the browser data in-depth, read my first blog post [here](https://www.paigeniedringhaus.com/blog/scrape-data-from-a-lazy-loading-website-with-selenium-python).

For me, this was things like: the NFT ID number (part of the NFT card's name) and the the NFT's sale price in BNB.

Inside of the ` __main__ ` method in my Python script, I'd scraped the data from the webpage with the `get_cards()` method, and then I wanted to loop through the NFT data I'd collected and extract the data points from each NFT card with my `get_nft_data()` method.

Here's the ` __main__ ` method code for reference:

**`for_sale_scraper.py`**

```python
if __name__ == ' __main__':
   scraper = ForSaleNFTScraper();
   cards = scraper.get_cards(max_card_count=200)
   card_data = []
   for card in cards:
    info = (scraper.get_nft_data(card)) 
    card_data.append(info)

    # pprint the card data to ensure we're getting the correct data out of each card
    pprint(card_data)    
```

And here is the code for my `get_nft_data()` method.

```python
def get_nft_data(self, nft_data):
        """Extracts and prints out NFT specific data."""
        nft_name_element = self.driver.find_element(By.XPATH, '//div[contains(@class, "Item_itemName__ckoHR")]')
        nft_name = nft_name_element.get_attribute("innerHTML")
        nft_id = nft_name.partition('#')[-1]

        nft_price_element = nft_data.find_element(By.XPATH, '//div[contains(@class, "Item_itemPriceValueTxt__lblqJ")]')
        nft_price = nft_price_element.get_attribute("innerHTML")
        return {
            'id': int(nft_id), 'price': nft_price
            }
```

What my `get_nft_data()` method is _supposed_ to do is:

1. Use the XPath within each card to get the NFT's name via [`get_attribute("innerHTML")`](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webelement.WebElement.get_attribute) (`innerHTML` targets the text content of the element which includes its ID number at the end of the name), [`partition()`](https://www.w3schools.com/python/ref_string_partition.asp) the string into a tuple based on the `#` included in the name, and take the last element in the tuple (which is the ID number).
2. It should also get the NFT's price (also using `get_attribute("innerHTML")`) via a second XPath within the NFT card.
3. Finally return those two elements together as a new object with the keys of `id` and `price`.

In theory, I wanted that to happen for every NFT I'd collected into my `card_data` list. In practice, I got 200 elements that all contained the ID and price data from the very first card in my `card_data` list.

Not quite what I was expecting.

### The Solution: How to restrict XPath inside a particular element

After several failed variations of the code above and searching through many Stack Overflow posts without success, I finally wrote my own SO post explaining my situation and asking for help from the greater web development community.

> If you'd like to see my original Stack Overflow post and the helpful responses provided, [here](https://stackoverflow.com/questions/75604911/python-web-scraping-with-selenium-only-extracts-first-elements-data-in-list-of) is a link.

Just over 30 minutes after posting my question, a kind soul answered it and got me moving forward again. That's the power of the web dev community at its finest.

Below is the corrected `get_nft_data()` code that actually gets the data from each individual NFT as the data is looped over. I also added some comments between lines of code to explain what's happening at each step.

```python
def get_nft_data(self, nft_data):
      """Extracts and prints out card specific data."""
      # get full card name "NFT_CARD #1234" by XPATH
      nft_name_element = nft_data.find_element(By.XPATH, './/div[contains(@class, "Item_itemName__ckoHR")]')
      nft_name = nft_name_element.text
      # parse out just ID number from name
      nft_id = nft_name.partition('#')[-1]

      # get nft recently sold value by XPATH
      nft_bnb_sale_price = nft_data.find_elements(By.XPATH, './/div[contains(@class, "Item_itemPriceValueTxt__lblqJ")]')
      
      # if there is a for sale price, take it 
      if nft_bnb_sale_price:
          nft_price = nft_bnb_sale_price[0].text
      else: 
      # if there's no value, just put None in place of a value    
          nft_price = None   

      return {
          'id': int(nft_id), 
          'nft_price': nft_price
          }
```

In this version of the Python code, there's three main differences.

1. The first is that instead of using `self.driver.find_element`, this code is using `nft_data.find_element`. By substituting `nft_data` instead of `self.driver`, it allows the XPath search to be restricted to a particular element.

2. Second, inside of each `find_element` method referencing `By.XPATH`, the XPath being passed has a `.` in front of it. So `'//div[contains(@class, "Item_itemName_ckoHR")]'` becomes `'.//div[contains(@class, "Item_itemName__ckoHR")]'`.

3. Last, the user suggested I could use `.text` to get the NFT name and BNB price instead of having to write out the lengthier `.get_attribute("innerHTML")` each time to reach the text in the NFT, which was a nice improvement in code readability.

The solution also mentioned there was a chance that some of the NFTs collected from the page may not have a price listed (NFTrade displays all NFTs in a collection, not just the ones for sale), and recommended wrapping the code that gets the `nft_price` in an `if / else` block so if the price is present, it will be collected and returned, and if it's not, it'll return `None` in place of the value and not throw an error in the code.

Hence this code for checking sale price:

```python
 # if there is a for sale price, take it 
      if nft_bnb_sale_price:
          nft_price = nft_bnb_sale_price[0].text
      else: 
      # if there's no value, just put None in place of a value    
          nft_price = None   
```

### Test the refactored code

With my newly refactored `get_nft_data()` method at the ready, it was time to test it out in my Python script.

As a reminder, my ` __main__ ` method looked like this:

```python
if __name__ == ' __main__':
   scraper = ForSaleNFTScraper();
   cards = scraper.get_cards(max_card_count=200)
   card_data = [] 
   for card in cards:
    info = (scraper.get_nft_data(card))
    card_data.append(info)

    # pprint the card data to ensure we're getting the correct data out of each card
    pprint(card_data)
```

This time, when I ran the script from the command line with `python for_sale_scraper.py`, here's a screenshot of the output I received.

[![Output from each of the NFT cards run through the get_nft_data() method](https://www.paigeniedringhaus.com/static/24031d6508cf9ab4329fdbcc01f27135/477c9/nft-xpath-data-output.png "Output from each of the NFT cards run through the get\_nft\_data() method")](/static/24031d6508cf9ab4329fdbcc01f27135/477c9/nft-xpath-data-output.png)

_If you look closely you'll see that each object in this area has a different ID and price in it._

As you can see from the image, I got an array of items and each item in the array had a different `id` and `nft_price` from the others. Now the `get_nft_data()` method was working correctly, targeting the next NFT in the `card_data` array with each successive iteration in the loop and pulling out the data specific to that card.

Success!

With that hurdle overcome, I was ready to move on to the next steps of this project: converting the NFT's BNB prices into the current USD prices and assembling them into a CSV spreadsheet. Those tasks will be covered in detail in upcoming blog posts.

---

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#conclusion)Conclusion

When I was asked to put together a spreadsheet of all the NFTs for sale in a particular collection on NFTrade, I ended up using Python to build a website scraper to accomplish it and learned a lot of new problem-solving techniques in the process.

I managed to load and collect all the NFT data from a web page with the assistance of the Selenium Python package, but I got throughly stuck when trying to iterate through my data to extract the ID and price for each NFT from it.

Luckily, the Stack Overflow community came through for me and taught me about the finer points of using Selenium WebDriver's XPath to target specific elements within a page instead of the whole page when looking for particular pieces of data, which got me unstuck and on my way to building my spreadsheet of data.

Thank goodness for the knowledge sharing of the online web development community - I am truly grateful to be able to turn to them when I've exhausted my own ideas to solve a problem.

Check back in a few weeks — I’ll be writing more blogs about the problems I had to solve while building this Python website scraper in addition to other topics on JavaScript or something else related to web development.

If you’d like to make sure you never miss an article I write, sign up for my newsletter here: https://paigeniedringhaus.substack.com

Thanks for reading. I hope learning how to restrict XPath searches to a particular element on a page instead of the entire page proves helpful for you in the future like was for me.

---

## Further References & Resources

- [Selenium Python](https://selenium-python.readthedocs.io/) docs
- [Selenium WebDriver](https://www.selenium.dev/documentation/webdriver/) docs
- [NFTrade](https://nftrade.com/) website
- [XPath](https://www.w3schools.com/xml/xml_xpath.asp) documentation
- Original [Stack Overflow post](https://stackoverflow.com/questions/75604911/python-web-scraping-with-selenium-only-extracts-first-elements-data-in-list-of)
- Previous blog post about [scraping data from a lazy-loading website using Selenium Python](https://www.paigeniedringhaus.com/blog/scrape-data-from-a-lazy-loading-website-with-selenium-python)