Handling the textbox and
button using selenium
Handling of Text box
Test Case Flow:
- Launches Chrome
- Navigates to a demo page
- Locates a text box via an XPath locator
- Checks if it’s enabled
- Enters text, pauses, then clears it
- Closes the browser
Program:
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:
- Setup & launch – points to your local chromedriver, then opens a new Chrome window.
- Navigate – goes to the URL where your button lives.
- Locate – finds the <button> via an XPath (change //button[@id=’submit’] to suit your page).
- Verify – prints out whether the button is visible on the page and enabled for clicking.
- Click – only clicks if both checks pass.
- Verify result – you can inspect the current URL or page title to ensure the click did what you expected.
- Teardown – quits the driver, closing all browser windows.
Program:
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();
}
}
}