Introduction to Handling Input Boxes with Selenium in Python

Automated web testing is a crucial part of the web development process, ensuring that users experience seamless and bug-free functionality. Selenium WebDriver is a powerful tool for automating web application testing, allowing developers to simulate user interactions with web pages. This lesson will guide you through handling input boxes in a form on a web page using Selenium with Python, using the practice website https://practiceautomatedtesting.com as a learning platform.

Getting Started with Selenium WebDriver

Before diving into handling input boxes, let’s set up Selenium WebDriver. Selenium WebDriver interacts with web browsers to execute tests. It requires a browser-specific driver; for Chrome, you’ll use the ChromeDriver.

Firstly, ensure you have Selenium and WebDriver Manager installed in your Python environment:

pip install selenium webdriver-manager

Here’s how you set up WebDriver to use with Chrome:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

# Setup WebDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

This code snippet automatically downloads the ChromeDriver executable and sets it up for use with Selenium.

Opening a Web Page

To work with input boxes, you first need to navigate to the web page containing the form. Here’s how to open a page:

driver.get("https://www.practiceautomatedtesting.com/webelements/FormButtonElements")
print("Opened practice automated testing")

Finding and Interacting with Input Boxes

Selenium provides various methods to locate elements on a web page. The most common are locating elements by their HTML attributes, such as name, ID, XPath, or CSS selector. Here’s how to find and interact with input boxes:

from selenium.webdriver.common.by import By

# Locate input field by XPath and enter text
inputusername = driver.find_element(By.XPATH, '//input[@name="name"]')
inputusername.send_keys('fetch CSS and XPATH the hard way')
print("Entered a name")

# Locate input email field by CSS Selector and enter text
inputemail = driver.find_element(By.CSS_SELECTOR, '[name="email"]')
inputemail.send_keys('info@learnautomatedtesting.com')
print("Entered an email")

# Locate input age field by XPath and enter text
inputage = driver.find_element(By.XPATH, '//input[@name="age"]')
inputage.send_keys('30')
print("Added an age")

This code finds the input fields for username, email, and age on the form and uses the send_keys method to simulate typing into these fields.

Submitting the Form

After filling out the form, the next step is to submit it. You can do this by finding the submit button and clicking on it:

btnsubmit = driver.find_element(By.CSS_SELECTOR,'[type="submit"]')
btnsubmit.click()

Verifying Submission Success

To ensure the form was submitted successfully, you can wait for a specific element that indicates success to appear on the page:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'All fields were filled successfully!')]"))
    )
    print("Text found on the page")
except:
    print("Text not found on the page")

This code snippet waits up to 10 seconds for the success message to appear, verifying that the form was submitted correctly.

Clean Up

Finally, it’s good practice to close the browser once your test or automation is complete:

driver.quit()

Conclusion

Handling input boxes with Selenium WebDriver in Python is straightforward once you understand how to locate elements and interact with them. This introduction has covered the basics of setting up Selenium, navigating to a web page, finding input elements, entering data, submitting a form, and verifying the outcome. With these skills, you’re well on your way to automating web testing for forms and beyond.