Tracking Crypto Prices with Ease: Selenium Automation with Python

Aurelien Lenoir
6 min readMar 13, 2023

Are you tired of constantly monitoring cryptocurrency prices and missing out on potentially lucrative opportunities?

In this article, we will discuss how to utilize the powerful automation tool, Selenium, along with the programming language Python to efficiently track crypto prices in real-time. With the ever-changing nature of the crypto market, it’s crucial to stay on top of the latest trends and fluctuations. By automating the process, you can save time and make informed decisions based on accurate data.

What is selenium?

Selenium is a library that can be found in Python and Java. It is used to perform tests using web navigation. However, it can be repurposed to perform everyday tasks using a script.

In this tutorial, we will see how to use Selenium with Python to log in to Instagram and create a project to retrieve the latest prices of the best cryptocurrencies.

How to install Selenium?

First, we will install Selenium. To do this, we must first have a Python environment and a virtual environment.

Once our development environment is ready to use, we just need to go to the virtual environment folder and launch it by running the command:

source <venv>/bin/activate # for linux
<venv>\\Scripts\\activate.bat # for windows

We just have to install the library by typing the command:

pip3 install selenium

To use Selenium, we must specify the browser we want to use.

Instead of using a Chromedriver, we can directly use a component of Selenium that contains several browsers such as Chrome, Firefox, Edge etc.

Once Selenium is installed, we can use a browser in our Python script. Here is an example that opens Instagram from Firefox:

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('<https://www.instagram.com/accounts/login/>')

# we add time to see that the page is displayed otherwise it would close directly
time.sleep(10)
Instagram login page
Standard browser display

Options

If we want to make changes to our browser, we can use options.

Here is a list of useful options:

  • Open in full screen
  • Use the “headless” mode, i.e. do not display the browser
  • Display certain logs, which can be useful during development

Here is a link that lists all possible options: https://peter.sh/experiments/chromium-command-line-switches/

To use them, we must import a component of the Selenium library: the Options.

from selenium import webdriver
# If we want to use another browser, we must change the webdriver import
from selenium.webdriver.chrome.options import Options

# We can add as many options as we want
options = Options()
options.add_argument('start-maximized')

driver = webdriver.Chrome(options=options)
driver.get('<https://www.instagram.com/accounts/login/>')
Instagram login page maximized
Full-screen browser display

Now that we can access the page we want, we will see how to interact with the browser.

We will use the Chrome development tools to retrieve information about the user and password fields.

User input from DOM
User input
Password input from DOM
Password input

As we can see, both have a name field that is unique, this is very convenient for us.

from selenium import webdriver
# If we want to use another browser, we must change the webdriver import
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
# We import our username and password
import config

# We can add as many options as we want
options = Options()
options.add_argument('start-maximized')

driver = webdriver.Chrome(options=options)
login_url = 'https://www.instagram.com/accounts/login/'

def connexion():
driver.get(login_url)
time.sleep(5)
accept_cookies()
time.sleep(5)

# We search for the username input
login = driver.find_element(By.NAME, 'username')
login.send_keys(config.USERNAME)

# We search for the username input
password = driver.find_element(By.NAME, 'password')
password.send_keys(config.PASSWORD)
password.send_keys(Keys.ENTER)

time.sleep(5)
decline_notification()

if driver.current_url == login:
print("Impossible to connect")
return False
else:
return True

def accept_cookies():
if driver.find_elements(By.CSS_SELECTOR, "button[class='_a9-- _a9_1']") != []:
cookies = driver.find_element(By.CSS_SELECTOR, "button[class='_a9-- _a9_1']")
cookies.click()
else:
print('No cookies')

def decline_notification():
if driver.find_elements(By.CSS_SELECTOR, "button[class='_a9-- _a9_1']") != []:
notif = driver.find_element(By.CSS_SELECTOR, "button[class='_a9-- _a9_1']")
notif.click()
else:
print('No notification')

if connexion():
time.sleep(3)
driver.quit()
else:
print('Connexion error')

For this script to work, you must create another file called config.py in which we will put our credentials.

You can find the full code here : https://github.com/TechIt-Ez/Instagram-Login

Cryptocurrencies Project

We will go to the website Cours Crypto.

First, we need to analyze the data we want to retrieve.

Cours Crypto main page
Data presentation

We can see that the table contains many columns and rows. However, not all columns are of interest to us. We want to retrieve the name of the cryptocurrency, its price, and its 24-hour percentage variation.

To do this, we will use the development tools of the browser to access the structure of the page.

To access them, we can click on the three dots in the top right of the browser, then select more tools and development tools.

Developer Tools
Chrome development tools

Once on the development tools, we can click on the small arrow in the top left of the tab and select the cryptocurrency table.

Cours Crypto table DOM
Select the table from the development tools

We can see a structure of the form <table> on the right tab. This structure interests us because it contains all the data we want to retrieve.

We need to browse the table to discover the elements we are looking for.

Each <table> is composed of a <thead> tag that symbolizes the table header, and a <tbody> tag that contains the table data.

We can see that the columns that interest us are columns 2, 3, and 4.

In the <tbody>, each line is symbolized by a <tr> tag that contains itself <td> tags (for each column).

Crypto data line in the DOM
<tr> tag for the line concerning Bitcoin

Here is the information that interests us:

Now that we know where the information is, it’s time to see how to retrieve it.

Crypto data columns in the DOM
Information we are looking for on the page

Selection methods

Selenium offers us the possibility to use different methods to select data:

  • ID: selects the id attribute
  • NAME: selects the name attribute
  • XPATH: selects the entire tag
  • LINK_TEXT: selects hyperlinks
  • PARTIAL_LINK_TEXT: selects hyperlinks
  • TAG_NAME: selects a certain attribute
  • CLASS_NAME: selects a certain class
  • CSS_SELECTOR: selects a certain style

In our example, we can see that the name of the cryptocurrency, the price, and the variation can be found thanks to the data-sort present in the <td> tags.

Let’s put it all into practice!

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

options=Options()
options.add_argument('start-maximized')
driver = webdriver.Chrome(options=options)
driver.get('https://www.courscrypto.com/')

time.sleep(10)

# We retrieve the full table
table = driver.find_element(By.ID, 'cmc_coinslist')
# We retrieve the data
table_body = table.find_element(By.XPATH, './tbody')
# We retrieve the list of lines
rows = table_body.find_elements(By.XPATH, './/*')

i = 1
# For each line we retrieve the name, price and variation
for row in rows:
# The [] allow to select the nth child of the component
crypto_name = row.find_element(By.XPATH, '//tr[{}]/td[2]'.format(i)).get_attribute('data-sort')
price = row.find_element(By.XPATH, '//tr[{}]/td[3]'.format(i)).get_attribute('data-sort')
variation = row.find_element(By.XPATH, '//tr[{}]/td[4]'.format(i)).get_attribute('data-sort')
print(crypto_name)
print(price)
print(variation)
# The i allows to increment the lines to retrieve the following values
i = i + 1
if i == 10:
break

driver.quit()
Cryptocurrencies name, price and variations
Results

You can find the full code here : https://github.com/TechIt-Ez/Crypto-Data

We just have to format it. We can also create a daily automatic task to check the Bitcoin price and then do statistical studies for example.

Here you can find an other article about automation.

I hope you enjoyed this article and it will give you the desire to create your own scripts to automate your daily tasks.

Follow me for more, and again checkout the Github if you wanna:

--

--