Skip to content
CodeKruPro

Click a Button

16/70

Video walkthrough

Watch on YouTube

Click a Button

The click() method simulates a left mouse click on an element.

On the Buttons page, the button with ID leftClick has text "Left click on me". When clicked, its text changes to "I was left-clicked!".

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Find the button by ID leftClick
  3. Print its text before clicking
  4. Click the button
  5. Print its text after clicking

Expected output:

Before: Left click on me
After: I was left-clicked!
Read full tutorial on Codekru

About this lesson

Click a button in Selenium Java with WebElement.click()

Selenium Java AssignmentsBeginnerWeb Element Interactions

How it works

element.click() scrolls the element into view and clicks its centre with the left mouse button, the way a user would. The browser fires the real events (mousedown, mouseup, click), so the page's own JavaScript runs exactly as it does for a person.

Before clicking, WebDriver checks that the element can receive the click. A hidden element throws ElementNotInteractableException, and an element covered by something else, such as a cookie banner or a modal backdrop, throws ElementClickInterceptedException and names the element that would have received the click instead.

A disabled button is a trap: on this page, clicking disabledButton throws nothing at all. The browser simply ignores the click. Check isEnabled() when the button's state matters to the test.

About the assignment

The Buttons page has a button that rewrites its own label when it gets a normal left click. Reading the label before and after the click is a small, complete check: find, read, act, read again.

What you will do

  1. Find the button by the id given in the task.
  2. Read its label with getText() and print it after the label Before:.
  3. Call click() on the same element.
  4. Read the label again from that element and print it after the label After:.

Common mistakes

  • Printing the value you saved before the click

    A String read before click() never changes. Call getText() again after the click; the element reference is still valid because the page only changed its text.

  • Expecting click() to double-click or right-click

    click() on the doubleClick or rightClick buttons leaves their labels unchanged, because those buttons listen for dblclick and contextmenu. Those need the Actions class from lessons 52 and 53.

  • Asserting right after a click that starts a request

    Here the label changes at once. On a real app a click often starts a network call, and an assertion on the next line can run before the page updates. Wait for the result with WebDriverWait, covered from lesson 36.

In a real test suite

Clicks drive most end-to-end tests: submitting forms, opening menus, moving through wizards. A common pattern is click, then wait for the visible effect, then assert, so the test checks what the user would see rather than only that the click happened.

Lesson 15 finished the locators. The next lesson moves from buttons to text fields and types into one with sendKeys().

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Buttons");
        
        // TODO: Find the leftClick button
        // TODO: Print text before clicking
        // TODO: Click the button
        // TODO: Print text after clicking
        
        driver.quit();
    }
}
Read the full tutorial