How to Handle Textboxes and Buttons in Selenium Python

How to Handle Textboxes and Buttons in Selenium Python

Selenium is a powerful tool for automating web applications. If you're learning Selenium with Python, one of the first things you'll want to do is interact with web elements like textboxes and buttons.

In this guide, we’ll walk you through:

  • Setting up Selenium

  • Locating elements (like textboxes and buttons)

  • Typing into textboxes

  • Clicking buttons

  • A complete working example


✅ Step 1: Install Selenium

First, install Selenium using pip:

pip install selenium

✅ Step 2: Download WebDriver

To control a browser (like Chrome), you need its driver.

Download ChromeDriver from:
https://sites.google.com/chromium.org/driver/

Make sure the ChromeDriver version matches your Chrome browser version.


✅ Step 3: Import Required Modules

from selenium import webdriver from selenium.webdriver.common.by import By import time
  • webdriver: Controls the browser

  • By: Helps locate elements by ID, name, class, etc.


✅ Step 4: Open a Browser

driver = webdriver.Chrome() # You can also use Firefox, Edge, etc. driver.get("https://example.com/login") # Replace with your URL

This opens the browser and navigates to the page.


๐Ÿ–Š️ Handling Textboxes (Input Fields)

Use send_keys() to type into a textbox.

username_box = driver.find_element(By.ID, "username") password_box = driver.find_element(By.NAME, "password") username_box.send_keys("myUsername") password_box.send_keys("myPassword")

Common Ways to Locate Elements:

Locator MethodExample
By.IDfind_element(By.ID, "user")
By.NAMEfind_element(By.NAME, "email")
By.CLASS_NAMEfind_element(By.CLASS_NAME, "box")
By.TAG_NAMEfind_element(By.TAG_NAME, "input")
By.LINK_TEXTfind_element(By.LINK_TEXT, "Login")
By.XPATHfind_element(By.XPATH, "//input")
By.CSS_SELECTORfind_element(By.CSS_SELECTOR, ".btn")

๐Ÿ”˜ Handling Buttons

Use .click() to simulate a button press.

login_button = driver.find_element(By.CLASS_NAME, "login-btn") login_button.click()

This clicks the button just like a real user would.


๐Ÿงช Full Working Example

from selenium import webdriver from selenium.webdriver.common.by import By import time # Setup Chrome browser driver = webdriver.Chrome() driver.get("https://example.com/login") # Replace with your real URL # Fill in the form username = driver.find_element(By.ID, "username") password = driver.find_element(By.NAME, "password") username.send_keys("testuser") password.send_keys("mypassword") # Click the login button login_button = driver.find_element(By.CLASS_NAME, "login-btn") login_button.click() # Wait for a few seconds to see the result time.sleep(5) # Close the browser driver.quit()

๐Ÿ’ก Bonus Tips

  • Always wait for elements to load (use time.sleep() or Explicit Waits with WebDriverWait)

  • Use unique identifiers (ID, name) when possible for more reliable element selection

  • Use developer tools (right-click → Inspect) to find element attributes

  • Handle invalid login or success messages using .text


๐Ÿง  Common Questions

๐Ÿ”น What if I want to clear the textbox before typing?

username.clear() username.send_keys("newuser")

๐Ÿ”น How can I submit a form without clicking a button?

password.submit() # Works if the textbox is inside a form

๐Ÿ”š Conclusion

Selenium makes it easy to interact with textboxes and buttons in Python. These basic actions form the core of most test cases—from login forms to checkout pages.

Once you understand these basics, you can move on to:

  • Dropdowns and checkboxes

  • Form validation

  • Waiting strategies

  • Page navigation


Comments

Popular posts from this blog

What is WebDriver in Selenium? Explained with Java

Tosca System Requirements and Installation Guide (Step-by-Step)

Why Choose Python for Full-Stack Web Development