Introduction
Buttons are the most commonly interacted element in web automation. Engineers must handle:
- Single click — basic
click()action - Double click — fires a different browser event
- Right click — opens the context menu
- Disabled button — assert it cannot be interacted with
Selenium uses the Actions class for advanced mouse interactions. Playwright has click options built in.
Key Methods Summary
| Action | Selenium (Java) | Playwright (JS) | Playwright (Python) |
|---|---|---|---|
| Single click | element.click() |
locator.click() |
locator.click() |
| Double click | actions.doubleClick(el).perform() |
locator.dblclick() |
locator.dblclick() |
| Right click | actions.contextClick(el).perform() |
locator.click({ button: "right" }) |
locator.click(button="right") |
| Is disabled | !isEnabled() |
toBeDisabled() |
to_be_disabled() |
| Get text | getText() |
textContent() |
text_content() |
1. Single Click
Selenium (Java)
driver.findElement(By.id("clickBtn")).click();
assertTrue(driver.findElement(By.id("clickResult")).isDisplayed());Playwright (JS)
await page.locator("#clickBtn").click();
await expect(page.locator("#clickResult")).toBeVisible();Playwright (Python)
page.locator("#clickBtn").click()
expect(page.locator("#clickResult")).to_be_visible()2. Double Click
Selenium (Java)
WebElement btn = driver.findElement(By.id("doubleClickBtn"));
new Actions(driver).doubleClick(btn).perform();
assertEquals("Double clicked!", driver.findElement(By.id("doubleClickResult")).getText());Playwright (JS)
await page.locator("#doubleClickBtn").dblclick();
await expect(page.locator("#doubleClickResult")).toHaveText("Double clicked!");Playwright (Python)
page.locator("#doubleClickBtn").dblclick()
expect(page.locator("#doubleClickResult")).to_have_text("Double clicked!")3. Right Click (Context Menu)
Selenium (Java)
WebElement btn = driver.findElement(By.id("rightClickBtn"));
new Actions(driver).contextClick(btn).perform();
assertTrue(driver.findElement(By.id("contextMenu")).isDisplayed());Playwright (JS)
await page.locator("#rightClickBtn").click({ button: "right" });
await expect(page.locator("#contextMenu")).toBeVisible();Playwright (Python)
page.locator("#rightClickBtn").click(button="right")
expect(page.locator("#contextMenu")).to_be_visible()4. Disabled Button — verify it cannot be clicked
Selenium (Java)
WebElement btn = driver.findElement(By.id("disabledBtn"));
assertFalse(btn.isEnabled());
assertNotNull(btn.getAttribute("disabled"));Playwright (JS)
await expect(page.locator("#disabledBtn")).toBeDisabled();Playwright (Python)
expect(page.locator("#disabledBtn")).to_be_disabled()5. Verify button text changes after click
Selenium (Java)
WebElement btn = driver.findElement(By.id("toggleBtn"));
String before = btn.getText();
btn.click();
assertNotEquals(before, btn.getText());Playwright (JS)
const btn = page.locator("#toggleBtn");
const before = await btn.textContent();
await btn.click();
expect(await btn.textContent()).not.toBe(before);Playwright (Python)
btn = page.locator("#toggleBtn")
before = btn.text_content()
btn.click()
assert btn.text_content() != before6. Click using keyboard Enter
Selenium (Java)
driver.findElement(By.id("submitBtn")).sendKeys(Keys.ENTER);Playwright (JS)
await page.locator("#submitBtn").press("Enter");Playwright (Python)
page.locator("#submitBtn").press("Enter")📄 Also Read: Top 10 Best Automation Practice Website