website, code, html-647013.jpg

How to automate device configuration via browser using Python and Selenium

How to automate device configuration via browser using Python and Selenium

Selenium is a browser automation library that is available for various programming languages, including Python. This tool allows you to take the user inputs associated with browser activity, like filling out forms, clicks, and so forth, and automate all that. This is particularly useful for automating various tasks, such as the upload of a configuration file to a network device.

For our case study today, we want to accomplish the following in short:

  • Have our program request we input the IP address we want to access.
  • Launch a web browser and log in to our network device.
  • Navigate the device menus to reach the page where we can upload our configuration.
  • Choose our configuration file and subsequently upload that file, flashing the device.

Request Target IP Address

#This script is designed to upload and flash a configuration file to our network device

#Import the necessary libraries
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#Request the IP address of the device to access
address = "http://"
address += input("Enter IP Address:")
print("The IP address you are accessing is", address)

Launch Web Browser and Log In

Our script has to find and interact with each element of the web page we are accessing. Selenium will read the code and you can use various methods to locate what element(s) to interact with. The key part of the script is using find_element_by_*() methods . Selenium will give you options to use methods such as find_element_by_name(), by_xpath(), by_link_text(), by_id(), and so on. You identify these in the web page code by using developer mode to find the identifiers of the elements.

In order to populate various text entry fields we will use the send_keys() method. The click() method is the “go” command and what is used to simulate a mouse click.

# Launch web browser, Chrome in this case
driver = webdriver.Chrome('./chromedriver.exe')

# Open the website
driver.get(address)

# Select the username form field
id_box = driver.find_element_by_name('name of username field')

# Populate username field
id_box.send_keys('enter username here')

# Find password form field
pass_box = driver.find_element_by_name('name of password field')

# Populate password field
pass_box.send_keys('enter password here')

# Find and click on login button. I used xpath here because finding by other methods didn't work.
login_button = driver.find_element_by_xpath('/html/body/form/div[2]/input[1]')
login_button.click()

Navigate Device Menus

# Find and click on first menu button. I used link_text for the next couple steps and input the name of the menu button.
system_button = driver.find_element_by_link_text('1stMenuButtonName')
system_button.click()

#Find and click on second menu button. Similar to above, navigating through the device menus.
backup_button = driver.find_element_by_link_text('2ndMenuButtonName')
backup_button.click()

Choose Configuration File from PC, Upload, Then Flash Device

#Find Choose File field and add backup file
choose_file_button = driver.find_element_by_id('choose_file')
choose_file_button.send_keys('C:/Users/Me/Documents/NameofConfigurationFile')

#Find and click on the upload button
upload_button = driver.find_element_by_name('upload')
upload_button.click()

Leave a Comment

Your email address will not be published. Required fields are marked *