How to Use Selenium Form WebElements

Key takeaways:

  • Selenium is an open-source suite for automated testing of web user interfaces.

  • WebElements included:

    • Text fields: Use .send_keys() to input text and .clear() to reset.

    • Buttons: Trigger actions with .click().

    • Checkboxes: Select with .click(), verify with .is_selected().

    • Radio buttons: Select with .click(), check status with .is_selected().

    • Dropdown lists: Manage selections using the Select class and .select_by_index().

    • File inputs: Upload files with .send_keys().

  • Locating elements are:

    • By.ID: Locate by HTML id.

    • By.name: Locate by HTML name.

    • By.CLASS_NAME: Locate by HTML class.

    • By.XPATH: Locate using XPath expressions.

Selenium is a free and open-source automated testing suite for web user interfaces. It provides automated testing for various types of web applications.

In Selenium, anything present on the web page is considered as WebElement. Form "WebElements" include "TextBox" and "Submit" buttons, which we can access by find_element() or find_elements() methods in the Selenium library. It can further fetch elements from the HTML webpage by following attributes:

  • By.ID: This locates an element by its HTML id attribute.

  • By.name: This finds an element by its HTML name attribute.

  • By.CLASS_NAME: This locates an element by its HTML class attribute.

  • By.XPATH: This locates an element using XPath expressions.

Selenium Form WebElements

The web elements included in the Selenium library for forms are as follows:

  • Text fields and text areas:

    • To enter text into input fields, you can use the .send_keys() method.

    • To clear the field with the .clear() method.

    • To locate the specific HTML element for this, you can inspect the webpage's source code and find the id.

Note: Don't forget to change the "username" and "message" with your username and message.

# Text Fields and Text Areas
text_field = driver.find_element(By.ID, "username")
text_field.send_keys("your_username")
text_area = driver.find_element(By.ID, "message")
text_area.send_keys("Hello, this is a message.")
Interacting with text fields in selenium
  • Buttons: Clickable elements like the "Submit" and "Reset" buttons are triggered by .click() method.

# Buttons
button = driver.find_element(By.ID, "submit_button")
button.click()
Interacting with buttons in selenium
  • Checkboxes: Toggleable options are selected using .click() method, and their checked state is verified with .is_selected() method in the library.

# Checkboxes
checkbox = driver.find_element(By.ID, "checkbox")
checkbox.click()
if checkbox.is_selected():
print("Checkbox is selected.")
Interacting with checkboxes in selenium
  • Radio buttons: You can choose exclusive options using the .click() method and check if an option is selected with the .is_selected() method.

# Radio Buttons
radio_button = driver.find_element(By.ID, "radio_button")
radio_button.click()
if radio_button.is_selected():
print("Radio button is selected.")
Interacting with radio buttons in selenium
  • Dropdown lists: You can control selectable choices within a dropdown using the Select method from the selenium.webdriver.support.ui module. You can use the .select_by_index() method to pick a specific option.

# Dropdown Lists (Select Element)
dropdown = Select(driver.find_element(By.ID, "dropdown"))
dropdown.select_by_index(2) # Selects the third option
Interacting with dropdown lists in selenium
  • Submit and reset buttons: You can use the .submit() method to submit form elements or specific buttons to reset forms. You can provide the button's id as an argument.

# Submit and Reset Buttons
submit_button = driver.find_element(By.ID, "submit_form")
submit_button.submit()
Interacting with submit button in selenium
  • File input fields: You can use the .send_keys() method to provide the file path for uploading files.

# File Input Fields
file_input = driver.find_element(By.ID, "file_upload")
file_input.send_keys("/path/to/your/file.txt")
Interacting with file input field in selenium

Code example

Here is the Python code example to see the real-time automation using Selenium. Press the "Run" button below in the widget to see Selenium Form WebElements:

from selenium import webdriver
import time
import warnings
warnings.filterwarnings("ignore")
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By 

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
prefs = {"download.default_directory": "."};

options.add_experimental_option("prefs", prefs);
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

driver.get('https://forms.gle/EK4sN2TCwSwTmFxC9');

try:
    time.sleep(3);
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '/html/body/div/div[2]/form/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input'))
    )

     #name
    text_field = driver.find_element(By.XPATH, '/html/body/div/div[2]/form/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
    text_field.send_keys("Optimus Prime")
    time.sleep(2);
    

    text_field = driver.find_element(By.XPATH, '/html/body/div/div[2]/form/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input')
    text_field.send_keys("DecepticonKiller@gmail.com")
    time.sleep(2);

    #gender
    # male = //*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div/span/div/div[1]/label/div/div[2]/div/span
    # female = //*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div/span/div/div[2]/label/div/div[2]/div/span
    # other = //*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div/span/div/div[2]/label/div/div[2]/div/span
    button = driver.find_element(By.XPATH, '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div/span/div/div[1]/label/div/div[2]/div/span')
    button.click()
    time.sleep(2);

    #occupation
    text_field = driver.find_element(By.XPATH, '/html/body/div/div[2]/form/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div[1]/div/div[1]/input')
    text_field.send_keys("InterGalactic Security")
    time.sleep(2);

    #Days to attend
    # 1 Day = //*[@id="mG61Hd"]/div[2]/div/div[2]/div[5]/div/div/div[2]/div[1]/div[1]/label/div/div[2]/div/span
    # 2 Day = /html/body/div/div[2]/form/div[2]/div/div[2]/div[5]/div/div/div[2]/div[1]/div[2]/label/div/div[2]/div/span
    # 3 Day = //*[@id="mG61Hd"]/div[2]/div/div[2]/div[5]/div/div/div[2]/div[1]/div[3]/label/div/div[2]/div/span
    button = driver.find_element(By.XPATH, '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[5]/div/div/div[2]/div[1]/div[3]/label/div/div[2]/div/span')
    button.click()
    time.sleep(2);


    #social media button
    button = driver.find_element(By.XPATH, '/html/body/div/div[2]/form/div[2]/div/div[2]/div[6]/div/div/div[2]/div/div/span/div/div[1]/label/div/div[2]/div/span')
    button.click()
    time.sleep(2);

    #submit
    button = driver.find_element(By.XPATH, '/html/body/div/div[2]/form/div[2]/div/div[3]/div[1]/div[1]/div/span/span')
    button.click()

     
except Exception as e:
    print("Timeout")
    print(e)
    driver.quit()


time.sleep(5);
driver.close();
Real time automation with selenium

Code explanation

  • Lines 1–8: Importing the required libraries.

  • Lines 10–18: Defining the setup for the automated browser.

  • Line 20: Opening the URL in the automated browser.

  • Lines 24–26: Here, we used the wait method till the desired element appeared on the screen.

  • Lines 27–67: Defining all the automated actions that are required to perform.

  • Lines 70–73: The exception block will catch all the possible errors and print them on the terminal.

  • Lines 76–77: After successful execution, the system waits for 5 seconds, and then it will close.

Conclusion

This Answer outlines how to interact with various form elements using Selenium. These methods allow us to automate tasks efficiently and enhance our testing capabilities for web applications.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can we handle form elements in Selenium?

Use methods like .send_keys() to input text, .click() for buttons, .clear() to reset fields and Select class for dropdowns.


How can we click on form in Selenium?

Locate the form element using a method like find_element() with an appropriate locator (e.g., By.ID, By.NAME), then use the .click() method to trigger it.


How can we locate WebElement by text?

Use XPath to locate elements by text. For example: driver.find_element(By.XPATH, "//tag[text()='desired text']").


Is WebElement a class or interface?

WebElement is an interface in the Selenium library.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved