Click actions in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class ClickActionTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { // Set path to chromedriver executable if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); } @Test public void testClickSubmitButtonNavigatesToConfirmation() { driver.get("https://example.com/form"); // Wait until Submit button is clickable WebElement submitButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn"))); // Click the Submit button submitButton.click(); // Wait until URL changes to expected confirmation page URL boolean urlChanged = wait.until(ExpectedConditions.urlToBe("https://example.com/confirmation")); // Assert URL is correct Assert.assertTrue(urlChanged, "URL did not change to confirmation page after clicking Submit button"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with Java and TestNG for assertions.
In setUp(), we start the Chrome browser and set an explicit wait of 10 seconds.
In the test method, we open the form page URL, then wait explicitly for the Submit button to be clickable using ExpectedConditions.elementToBeClickable. This avoids timing issues.
We then click the Submit button and wait until the URL changes to the expected confirmation page URL using ExpectedConditions.urlToBe.
Finally, we assert that the URL did change correctly, ensuring the click action worked as expected.
In tearDown(), we close the browser to clean up.
This approach follows best practices: using explicit waits, proper locators, and clear assertions.
Now add data-driven testing with 3 different form URLs and verify clicking Submit navigates to their respective confirmation pages.
Practice
click() method do in Selenium WebDriver?Solution
Step 1: Understand the purpose of
Theclick()click()method is designed to simulate a user clicking on a web element like a button or link.Step 2: Differentiate from other actions
Typing text usessendKeys(), navigation usesget(), and finding elements usesfindElement(). None of these perform clicks.Final Answer:
Simulates a user clicking on a web element -> Option AQuick Check:
click() = Simulate click [OK]
- Confusing click() with sendKeys()
- Thinking click() navigates pages
- Using click() without locating element first
Solution
Step 1: Locate element using
In Selenium Java, to interact with an element, you first find it usingfindElement(By.id())driver.findElement(By.id("submitBtn")).Step 2: Call
After locating, you callclick()on the found elementclick()on the WebElement to simulate the click.Final Answer:
driver.findElement(By.id("submitBtn")).click(); -> Option CQuick Check:
findElement + click() = correct syntax [OK]
- Trying to call click() directly on driver
- Using sendKeys("click") instead of click()
- Incorrect method names like clickElement()
driver.findElement(By.id("loginBtn")).click();Solution
Step 1: Understand element visibility requirement for click()
Selenium requires the element to be visible and enabled to perform a click. If the element is hidden, click() fails.Step 2: Identify the exception thrown
If the element is found but not visible, Selenium throwsElementNotVisibleExceptionorElementNotInteractableException, notNoSuchElementException.Final Answer:
Throws ElementNotVisibleException or ElementNotInteractableException -> Option BQuick Check:
Click requires visible element = Exception if hidden [OK]
- Assuming click works on hidden elements
- Confusing NoSuchElementException with visibility exceptions
- Ignoring element state before clicking
WebElement button = driver.findElement(By.name("submit"));
button.click();Assuming the element exists but the test fails with
ElementNotInteractableException.Solution
Step 1: Confirm locator correctness
The locatorBy.name("submit")finds the element, so no error here.Step 2: Understand cause of ElementNotInteractableException
This exception means the element is present but not interactable, usually because it is hidden or disabled.Final Answer:
The element is not visible or enabled to be clicked -> Option AQuick Check:
ElementNotInteractable = element hidden or disabled [OK]
- Assuming locator is wrong without checking
- Thinking click() method is misspelled
- Ignoring element visibility and enabled state
Solution
Step 1: Understand hover-triggered elements
Buttons that appear only after hovering require simulating mouse movement to reveal them.Step 2: Use Actions class to hover and then click
The SeleniumActionsclass can move the mouse to the menu element, triggering the button to appear, then click it.Step 3: Why other options fail
Direct click fails because button is hidden. sendKeys ENTER does not hover. Refreshing page does not reveal button.Final Answer:
Use Actions class to move to the menu, then click the button -> Option DQuick Check:
Hover with Actions before click = correct [OK]
- Clicking hidden elements without hover
- Using sendKeys instead of mouse actions
- Refreshing page expecting button to appear
Ready to prove it?
Pass the test and this pattern moves unknown
