Automating Web Actions with Python and Selenium: Streamline Your Workflows

In this guide, we will focus on web automation using Python and Selenium, using OrangeHRM as our testing website. Our goal is to automate tasks like logging in, modifying a field, and verifying the amendment is successful. By harnessing the power of Python and Selenium, we'll streamline these actions and uncover the efficiency of web automation. Let's get started and embark on this journey of automation!

Step 1: Setting up the Environment

To ensure you have Python 3 and Selenium installed on your system, follow the steps below:

  1. Open your command prompt or terminal.
  2. Check if Selenium is installed by running the command:
pip3 list | grep selenium
  • If Selenium is installed, you will see its version and related packages listed.
  • If not installed, you can install it using the command: pip3 install selenium.
  1. Verify your Python version by running the command:
python3 --version
  • This command will display the installed Python version. Ensure it is Python 3.x (e.g., 3.7, 3.8, 3.9).

By performing these steps, you can confirm that both Python 3 and Selenium are installed on your system. This verification is essential before proceeding with web automation using Python and Selenium.

Step 2: Testing the Script

Copy the following Python script and run it to test the script. This script will perform a series of actions, showcasing the automation capabilities

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import random
import time

# Initialize ChromeDriver
driver = webdriver.Chrome()

# Navigate to the OrangeHRM login page
driver.get('https://opensource-demo.orangehrmlive.com/web/index.php/auth/login')
time.sleep(1)

# Find and enter the Username
driver.find_element(By.NAME, 'username').send_keys('Admin')
time.sleep(1)

# Find and enter the Password
driver.find_element(By.NAME, 'password').send_keys('admin123')
time.sleep(1)

# Click the Login button
driver.find_element(By.TAG_NAME, "button").click()
time.sleep(1)

# Extract information from the page before making changes
text = driver.find_element(By.CLASS_NAME, 'oxd-userdropdown-name').text
print("Username (Before): " + text)
time.sleep(1)

# Go to the My Info page
driver.find_element(By.LINK_TEXT, 'My Info').click()
time.sleep(1)

# Generate a random word to replace the Lastname
word_file = "/usr/share/dict/words"
WORDS = open(word_file).read().splitlines()
word = random.choice(WORDS)

# Replace the Lastname field with the generated word
lastName = driver.find_element(By.NAME, 'lastName')
lastName.click()
time.sleep(1)
lastName.send_keys(Keys.COMMAND + "a")
lastName.send_keys(Keys.DELETE)
time.sleep(1)
lastName.send_keys(word)
time.sleep(1)

# Click the Save Button
driver.find_element(By.XPATH, '//button[text()=" Save "]').click()
time.sleep(2)

# Refresh the page
driver.refresh()
time.sleep(2)

# Extract information from the page after making changes
text = driver.find_element(By.CLASS_NAME, 'oxd-userdropdown-name').text
print("Username (After): " + text)
time.sleep(1)

# Close the browser
driver.quit()

Once you execute the script, you will observe the browser window launching, the automation actions being performed, and the output displaying the username before and after the changes made.

Conclusion:

Congratulations! You have successfully executed the script, automating web actions using Python and Selenium. This script demonstrates the power of automation by logging into OrangeHRM, updating information, and extracting data from the web page. Feel free to explore and modify the script to suit your specific automation needs.

Note: Please ensure that you have a stable internet connection and that the OrangeHRM website is accessible while executing the script. It is important to note that OrangeHRM is the property of its rightful owner, and this script is solely for educational and demonstration purposes. If there are any concerns regarding copyright or other related matters, please reach out via email at [cheehau@bizportal.cc].