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
- Find the button by the id given in the task.
- Read its label with
getText()and print it after the labelBefore:. - Call
click()on the same element. - 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
Stringread beforeclick()never changes. CallgetText()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 thedoubleClickorrightClickbuttons leaves their labels unchanged, because those buttons listen fordblclickandcontextmenu. 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().
Related lessons
- Double Click ActionTrigger a real double-click, which
click()cannot do. - isEnabled() - Check Enabled StateCheck
isEnabled()before clicking a button that may be disabled. - Click Using JavaScriptClick through JavaScript when an overlay intercepts a normal click.
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.
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();
}
}