By continuing to use our website, you consent to the use of cookies. Please refer our cookie policy for more details.

    Automating Different Input Fields Using Selenium Webdriver

    Input fields like textboxes, radio buttons, and check boxes are present in almost all web applications and automating them is the most important part of automation testing. In a data-driven approach to automation, scripts are designed to accommodate variable data. This data can be kept in various external data sources e.g. database, excel, csv files, etc. Here, we are using excel as the data source to show automation of different input fields using Selenium Webdriver.

    In all the below examples of input fields,

    Following test data is used:

    Test Data for Selenium Webdriver

    Following code snippets are used:

    locator – By.id(ID_OF_SPECIFIC_ELEMENT)
    value – value for specific input field from the test data above

    1. Text box/Text area

    A text box is a box that allows you to enter text in that section. Text box and area are similar but the latter allows for higher character limit and multiple lines

    Text Box selenium input fields

    WebElement field = driver.findElement(locator);
    field.clear();
    field.sendKeys(value);

    1. Radio Button

    A radio button is a two-state button that can be either checked or unchecked. Users can select only one option from the set of options.

    Radio button selenium input fields

    List<WebElement> select = driver.findElements(locator);

    for (WebElement radio : select){
    if (radio.getAttribute(“value”).equalsIgnoreCase(value))
    radio.click();
    }

    1. Checkboxes

    Checkboxes permit user to make multiple selections from a number of options.
    The following code selects multiple checkboxes with input value separated by commas
    e.g.value = Cricket, Lan Gaming, Listening.

    checkboxes selenium input fields

    List<WebElement>abc = driver.findElements(locator);
    List<String> list = new ArrayList<String>(Arrays.asList(value.split(“,”)));

    for (String check : list){
    for (WebElementchk : abc){
    if(chk.getAttribute(“value”).equalsIgnoreCase(check))
    chk.click();
    }
    }

    1. Drop-down

    This is a simple drop-down, where a list of values is displayed on clicking it and user may select one.

    dropdown selenium input fields

    Selectselect= new Select (driver.findElement(locator));
    select.selectByVisibleText(value);

    1. Auto-suggest Search Drop Down

    These drop-downs have the option of selecting a value from the list as well as search and select from the resultant data.

    Auto suggest search selenium input fields

    driver.findElement(locator).click();
    driver.findElement(locator).sendKeys(value);
    driver.findElement(locator).sendKeys(Keys.TAB);

    1. File Upload

    Most of the file upload options are displayed with a box and a browse button. Click on either one and
    you get a dialog box to select a file from your system. In the following code, value is the location of the file that you want to upload.

    File upload selenium input fields

    driver.findElement(locator).sendKeys(value);

    Checkout Grazitti’s Selenium Webdriver Framework Version 3, where we have automated all the above mentioned input fields in one of our Test Scripts – RegistrationTest.java .You can also get the parameterization code – used for fetching values from excel sheet from our framework.

    We welcome your feedback on this article as well as on our Selenium Webdriver Framework.