Automate NordVpn With !Selenium Python — Automation Tutorial

Selmi Abderrahim
4 min readNov 26, 2020

Nordvpn comes with over 5000 servers around the world and choosing the fastest server within all of these is quite a challenge, so NordVpn provides to us this tool that gives us the recommended server based on our location, you can choose one of the 60 countries available here. What we will do today is building a script using Selenium to get a list of 60 server names, the best server from each country.

Introduction:

The format of our project looks as follow:

We have:

  • best_servers: Where we will save the list of the best server names
  • geckodriver: Is the web browser engine which we will use in our tutorial, we have one for Linux, one for Windows, and another one for Mac system.
  • script.py: Where we will write our script

Note: You can download the entire project from Github. (https://github.com/SelmiAbderrahim/NordVpn-Best-Servers-Automation)

The code

In case you’re in hurry and you need only the code, here is it:

#Things we need to import
from selenium import webdriver
import platform
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.keys import Keys
#headless method to hide the browser's window (True)
op = Options()
op.headless = False
#The platform.system to define the system we use
what_os = platform.system()
#Use the right browser for each system
if what_os == "Linux":
driver = webdriver.Firefox(executable_path="linux64/geckodriver", options=op)
elif what_os == "Windows":
driver = webdriver.Firefox(executable_path="win64/geckodriver.exe", options=op)
elif what_os == "Darwin":
driver = webdriver.Firefox(executable_path="macos/geckodriver", options=op)
#navigate the nordvpn tools page
url = "https://nordvpn.com/servers/tools/"
driver.get(url)
#where we will save the server names
file = open("best_servers", "a")
#loop over each country
for i in range(2, 60):
#locate the dropdown menu
dropdown = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@role='combobox']")))
#open the dropdown menu
ActionChains(driver).move_to_element(dropdown).click().perform()
time.sleep(3)
#scroll down
ActionChains(driver).send_keys(Keys.ARROW_DOWN).perform()
time.sleep(2)
#locate each country element
country = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@role='combobox']//div[@data-id='%d']" %i)))
#click on each country
ActionChains(driver).move_to_element(country).click().perform()
time.sleep(3)
#get the name of the server
name = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//p[@class='Text micro mb-7']")))
#save server names ina file
file.write(name.text+"\n")
file.close()
driver.close()

Going through the code

from selenium import webdriver
import platform
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.keys import Keys

The first thing we do here is importing some packages we need in our project:

  • Options: the options class from firefox we will use it to set the headless property True, to use the browser headlessly.
  • WebDriverWait: it allows the code to wait for a certain condition to be valid before proceeding further.
  • By: is used to find elements within a document.
  • EC: the expected conditions, which we will use to tell the web driver to stop waiting because our condition is valid.
  • ActionsChains: it is used for more complex actions like hover over and drag and drop.
  • Keys: the selenium special keys
op = Options()
op.headless = False

The headless property here allows us to hide the browser’s window in case it’s set to true or to show the window if it’s set to False.

what_os = platform.system()
if what_os == "Linux":
driver = webdriver.Firefox(executable_path="linux64/geckodriver", options=op)
elif what_os == "Windows":
driver = webdriver.Firefox(executable_path="win64/geckodriver.exe", options=op)
elif what_os == "Darwin":
driver = webdriver.Firefox(executable_path="macos/geckodriver", options=op)

The platform.system() returns the name of the system you’re using, so we use this feature to tell Python which Geckodriver file it should use based on the system it finds on what_os variable.

url = "https://nordvpn.com/servers/tools/"
driver.get(url)
file = open("best_servers", "a")

Here, we simply saved the URL of the Nordvpn servers tools and we navigated to it using the .get method.

Then we defined a file where we will save the list of the recommended servers.

for i in range(2, 60): #[1]
#[2]
dropdown = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@role='combobox']")))

ActionChains(driver).move_to_element(dropdown).click().perform()
time.sleep(3)
#[3]
ActionChains(driver).send_keys(Keys.ARROW_DOWN).perform()
time.sleep(2)
#[4]
country = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@role='combobox']//div[@data-id='%d']" %i)))
#[5]
ActionChains(driver).move_to_element(country).click().perform()
time.sleep(3)
#[6]
name = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//p[@class='Text micro mb-7']")))
#[7]
file.write(name.text+"\n")

[1]: We will loop over the 58 server name.

[2]: We used the By class to locate the dropdown menu and the AcionChains to click on it. Because for each server we need to open the dropdown menu to reach the server names of each country.

[3]: And to make sure our driver reaches each one of all the server names, we need to scroll down to that specific element each time we open the dropdown menu, and we do that using the selenium special keys.

[4] and [5]: We located the elements using the XPath, each country has unique data-id, so we used that to find them and click on each one of them.

[6]: We saved the name of the recommended server in a variable “name”, then write it in a new line in the txt file [7].

file.close()
driver.close()

One last thing we need to do is closing the file and the driver.

Watch the tutorial on Youtube: https://youtu.be/EaWWJohfwAU

--

--

Selmi Abderrahim

I’m a computer science student. I do freelancing in my free time. I am a big fan of affiliate marketing, it helped me a lot to monetize my daily stuff.