Here's a multimodal Selenium testing example in Python that handles different types of interactions:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
import os
# Initialize WebDriver
driver = webdriver.Chrome()
driver.maximize_window()
try:
# Navigate to a test site
driver.get("https://the-internet.herokuapp.com/")
# TEXT INPUT TESTING
driver.get("https://the-internet.herokuapp.com/login")
driver.find_element(By.ID, "username").send_keys("tomsmith")
driver.find_element(By.ID, "password").send_keys("SuperSecretPassword!")
driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
# Verify success message appears
success_message = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "flash.success"))
)
assert "You logged into a secure area!" in success_message.text
# IMAGE VERIFICATION
driver.get("https://the-internet.herokuapp.com/broken_images")
images = driver.find_elements(By.TAG_NAME, "img")
# Check if images are loaded properly
for img in images:
if img.get_attribute("naturalWidth") == "0":
print(f"Broken image found: {img.get_attribute('src')}")
else:
print(f"Image loaded successfully: {img.get_attribute('src')}")
# FILE UPLOAD
driver.get("https://the-internet.herokuapp.com/upload")
# Create a temporary file to upload
with open("test_upload.txt", "w") as f:
f.write("This is a test file for Selenium upload")
# Upload the file
file_path = os.path.abspath("test_upload.txt")
driver.find_element(By.ID, "file-upload").send_keys(file_path)
driver.find_element(By.ID, "file-submit").click()
# Verify upload success
upload_success = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "uploaded-files"))
)
assert "test_upload.txt" in upload_success.text
# DROPDOWN INTERACTION
driver.get("https://the-internet.herokuapp.com/dropdown")
dropdown = driver.find_element(By.ID, "dropdown")
dropdown.click()
driver.find_element(By.CSS_SELECTOR, "option[value='1']").click()
# Verify selection
selected_option = driver.find_element(By.CSS_SELECTOR, "option[selected='selected']")
assert selected_option.text == "Option 1"
# ALERT HANDLING
driver.get("https://the-internet.herokuapp.com/javascript_alerts")
driver.find_element(By.XPATH, "//button[text()='Click for JS Alert']").click()
# Switch to alert and accept it
alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
alert_text = alert.text
alert.accept()
# Verify result
result = driver.find_element(By.ID, "result")
assert "You successfully clicked an alert" in result.text
# Clean up the test file
os.remove("test_upload.txt")
print("All multimodal tests completed successfully!")
except Exception as e:
print(f"Test failed: {str(e)}")
finally:
# Close browser
driver.quit()
Would you like me to explain or break down this code?