Have a question?
Message sent Close
View Categories

Handling the textbox and button using selenium

📄
filename.js
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();

        }

    }

}

📄
filename.js
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();

        }

    }

}