1. Technical Basics
Filling Out and Submitting Forms
Working with forms is often needed for automating user registration, filling out questionnaires, collecting data, and much more. When you need to fill out hundreds of forms, doing it manually is like trying to drink a watermelon through a straw. But with our Selenium superpowers, we’ll get it done in seconds.
Let's start by figuring out how we can fill in text fields and submit forms. We'll learn the basic steps with an example of a typical login form:
from selenium import webdriver
# Setting up the driver (e.g., ChromeDriver)
driver = webdriver.Chrome()
try:
# Open the web page
driver.get("https://example.com/login")
# Find the username input field and type in text
username_input = driver.find_element_by_name("username")
username_input.send_keys("my_login")
# Find the password input field and type in text
password_input = driver.find_element_by_name("password")
password_input.send_keys("my_password")
# Find the submit button and click it
submit_button = driver.find_element_by_name("submit")
submit_button.click()
finally:
# Close the driver
driver.quit()
Validation and Error Handling
Nothing ruins a programmer's day faster than errors popping up at the wrong moment. So, we handle them in advance. Use try/except to catch errors and log them to figure out what went wrong.
try:
username_input = driver.find_element_by_name("username")
username_input.send_keys("my_login")
except Exception as e:
print(f"Error filling out username: {e}")
logging.error("Error filling out username", exc_info=True)
2. Checkboxes
Now that we've learned to click buttons, let's try selecting multiple options from a list. To check or uncheck a checkbox, you can use the click() method. If you need to check whether a checkbox is selected, use the is_selected() attribute.
checkbox = driver.find_element_by_id("checkbox_id")
if not checkbox.is_selected():
checkbox.click()
3. Radio Buttons
Radio buttons allow you to select only one option from a set. They work similarly to checkboxes: select an option using the click() method and check its state using is_selected().
radio_button = driver.find_element_by_id("radio_button_id")
if not radio_button.is_selected():
radio_button.click()
4. Dropdowns
Dropdowns contain several options to choose from. Selenium has a Select class that provides methods for working with them.
from selenium.webdriver.support.ui import Select
dropdown = Select(driver.find_element_by_id("dropdown_id"))
dropdown.select_by_visible_text("Option 1")
You can also select options using select_by_index() or select_by_value().
5. Text Areas
Text areas are similar to text fields but are typically used for entering large amounts of text. Working with them is identical to working with text fields.
text_area = driver.find_element_by_id("textarea_id")
text_area.send_keys("This is a sample text for the text area.")
6. Date Picker
Date picker fields allow users to select dates. If it’s a standard text field supporting the date format, you can use send_keys() to input a value in the desired format.
date_field = driver.find_element_by_id("date_field_id")
date_field.send_keys("2023-12-25") # Date format might vary depending on the site
However, some date picker fields require additional actions, like opening a popup calendar and selecting a date.
7. File Upload
These fields let you upload files to the server. Interact with them using the send_keys() method, specifying the file path on your local computer.
file_input = driver.find_element_by_id("file_upload_id")
file_input.send_keys("/path/to/file.txt")
8. Hidden Fields
Hidden fields aren’t visible on the page but can be used to store information needed for form submission. You can interact with them only if you have access to their value (e.g., for checking or modifying the value).
hidden_field = driver.find_element_by_id("hidden_field_id")
value = hidden_field.get_attribute("value")
print("Hidden field value:", value)
9. Checking Action Success
Getting feedback on your actions is your success barometer. After submitting a form, you’ll definitely want to confirm that the application was successfully submitted, right? Here’s how you can do that:
># Checking successful login to the site
if "Welcome" in driver.page_source:
print("Login was successful!")
else:
print("An error occurred during login.")
10. Issues and Solutions
Issues
- Unstable Performance: Web pages frequently change. Changes to the page structure can cause script failures, so regular code checks are necessary.
- Network Speed Dependency: Page load time and element rendering can vary. Use explicit waits to handle delays.
Solutions
- Logging: Log all actions to better understand issues. Using the
logginglibrary makes this much easier. - Code Modularity: Break complex actions into functions and modules to make changes and testing simpler.
GO TO FULL VERSION