Handling Input Boxes, Buttons, and Checkboxes in Selenium
Handling Input Boxes, Buttons, and Checkboxes in Selenium
Selenium is a powerful tool for automating web browsers. It allows you to simulate user interactions like typing in input boxes, clicking buttons, and checking checkboxes.
This guide will walk you through how to handle these elements using Python with Selenium.
✅ 1. Set Up Selenium
Install Selenium
bash
Copy
Edit
pip install selenium
Download WebDriver
Download the WebDriver for your browser (e.g., ChromeDriver for Chrome).
Make sure it's in your system path or specify the full path in your code.
๐ฅ 2. Handling Input Boxes
To type text into an input field:
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
# Locate the input box by ID (can also use name, class, etc.)
input_box = driver.find_element(By.ID, "username")
# Clear existing text and enter new text
input_box.clear()
input_box.send_keys("my_username")
๐ 3. Handling Buttons
To click a button:
python
Copy
Edit
# Locate the button
button = driver.find_element(By.ID, "submit")
# Click the button
button.click()
If the button is inside a form, this might also submit the form.
☑️ 4. Handling Checkboxes
To check or uncheck a checkbox:
python
Copy
Edit
checkbox = driver.find_element(By.ID, "remember_me")
# Check if it's already selected
if not checkbox.is_selected():
checkbox.click() # This will check the box
You can also uncheck it by:
python
Copy
Edit
if checkbox.is_selected():
checkbox.click()
๐ก 5. Locators You Can Use
You can find elements using different types of locators:
python
Copy
Edit
driver.find_element(By.ID, "element_id")
driver.find_element(By.NAME, "element_name")
driver.find_element(By.CLASS_NAME, "class_name")
driver.find_element(By.TAG_NAME, "input")
driver.find_element(By.LINK_TEXT, "Click here")
driver.find_element(By.XPATH, "//input[@type='text']")
driver.find_element(By.CSS_SELECTOR, "input[name='email']")
๐ ️ 6. Waits (Best Practice)
Use waits to ensure elements are loaded before interacting with them:
python
Copy
Edit
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "username")))
✅ Example: Complete Login Automation
python
Copy
Edit
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
driver = webdriver.Chrome()
driver.get("https://example.com/login")
wait = WebDriverWait(driver, 10)
# Input username and password
wait.until(EC.presence_of_element_located((By.ID, "username"))).send_keys("my_username")
driver.find_element(By.ID, "password").send_keys("my_password")
# Check the "Remember Me" checkbox
checkbox = driver.find_element(By.ID, "remember_me")
if not checkbox.is_selected():
checkbox.click()
# Click login button
driver.find_element(By.ID, "login_button").click()
๐งน 7. Clean Up
Don’t forget to close the browser at the end:
python
Copy
Edit
driver.quit()
๐ง Conclusion
Selenium makes it easy to automate web interactions:
Use .send_keys() for input boxes.
Use .click() for buttons and checkboxes.
Use waits to handle page load delays.
With these basics, you can start automating login forms, search bars, filters, and more.
Would you like a downloadable Python script template or help with a specific site or form?
Learn Selenium Python Training in Hyderabad
Read More
Working with Browser Navigation in Selenium using Python
Working with Browser Navigation in Selenium using Python
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment