Click Send Keys and Clear

The Three Basic Actions

WebElement search = driver.findElement(By.id("search"));
search.clear();                       // remove any old text
search.sendKeys("bluetooth speaker"); // type like a user
search.sendKeys(Keys.ENTER);          // press Enter (import org.openqa.selenium.Keys)

driver.findElement(By.cssSelector(".product-card h3")).click();   // open the first product
driver.findElement(By.id("add-to-cart")).click();

Example

After these steps the browser is on the product page and the cart badge shows 1:

Run AddToCartTest.main()

Example output

Cart count: 1

When a Click Fails

Selenium clicks like a real user, so the element must be visible and not covered by something else (a cookie banner, a loading spinner).

Example

Clicking a button hidden under a popup throws:

Run AddToCartTest.main()

Example output

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <button id="add-to-cart" class="btn btn-primary">...</button> is not clickable at point (640, 512). Other element would receive the click: <div class="cookie-banner">...</div>

Tip: Close the popup first, or wait for the spinner to disappear (waits are in the medium level).