---
title: Handling the textbox and button using selenium
description: "**Handling the textbox and** **button using selenium** **Handling of Text box ** Test Case Flow:  1. Launches Chrome 2. Navigates to a demo page 3. Locates a text box via an XPath locator 4. Checks if it’s enabled 5. Enters text,…"
url: "https://vinothqaacademy.com/docs/handling-the-textbox-and-button-using-selenium/"
updated: "2025-09-26"
category: Selenium Automation Testing
tags:
  - Selenium Automation Testing
---

# Handling the textbox and button using selenium

**Handling the textbox and**

**button using selenium**

**Handling of Text box **

Test Case Flow: 

1. Launches Chrome
2. Navigates to a demo page
3. Locates a text box via an XPath locator
4. Checks if it’s enabled
5. Enters text, pauses, then clears it
6. Closes the browser

*Program:*

```javascript

                import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class TextBoxExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page containing your text box

            driver.get("https://example.com/login");  

            // 4. Locate the text box using an XPath locator

            //    (adjust the XPath to match your page)

            WebElement usernameField = driver.findElement(

                By.xpath("//input[@id='username']"));

            // 5. Check if the element is enabled

            boolean isEnabled = usernameField.isEnabled();

            System.out.println("Username field enabled? " + isEnabled);

            if (isEnabled) {

                // 6. Enter some text

                usernameField.sendKeys("myTestUser");

                // (just for demo) wait so you can see it happen

                Thread.sleep(2000);

                // 7. Clear the text

                usernameField.clear();

                Thread.sleep(1000);

            }

        } finally {

            // 8. Close the browser (and end the session)

            driver.quit();

        }

    }

}
```

***Notes:***

- Replace "/path/to/chromedriver" with the actual path on your machine (or use WebDriverManager).
- Adjust the URL (https://example.com/login) and the XPath (//input[@id='username']) to match your application under test.
- The Thread.sleep(...) calls are only for demonstration—typically you’d use explicit waits instead.

***Handling*of Button **

*Test Case Flow: *

1. Setup & launch – points to your local chromedriver, then opens a new Chrome window.
2. Navigate – goes to the URL where your button lives.
3. Locate – finds the <button> via an XPath (change //button[@id='submit'] to suit your page).
4. Verify – prints out whether the button is visible on the page and enabled for clicking.
5. Click – only clicks if both checks pass.
6. Verify result – you can inspect the current URL or page title to ensure the click did what you expected.
7. Teardown – quits the driver, closing all browser windows.

*Program:*

```javascript

                import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class ButtonExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page containing your button

            driver.get("https://example.com");  

            // 4. Locate the button using an XPath locator

            //    (adjust the XPath to match your page)

            WebElement submitBtn = driver.findElement(

                By.xpath("//button[@id='submit']"));

            // 5. Check if the button is displayed and enabled

            System.out.println("Button displayed? " + submitBtn.isDisplayed());

            System.out.println("Button enabled?   " + submitBtn.isEnabled());

            // 6. Click the button if possible

            if (submitBtn.isDisplayed() && submitBtn.isEnabled()) {

                submitBtn.click();

                // 7. (Optional) Pause so you can see the result

                Thread.sleep(2000);

                // 8. (Optional) Verify navigation or change

                System.out.println("Current URL after click: " + driver.getCurrentUrl());

            }

        } finally {

            // 9. Close the browser (and end the session)

            driver.quit();

        }

    }

}
```
