Search code examples
javaseleniumselenium-webdriverkeysendkeys

How to press the delete button in selenium using java?


I have try to press the delete key using below command but it doesn't reflect anything!

element = driver.findElement(By.cssSelector((#DeleteThis)));
element.sendKeys(Keys.DELETE);

Solution

  • You need to build an action and the send it, the send keys is bound to actions not elements, example :

    Actions action = new Actions(yourDriver);
    
    action.sendKeys(Keys.DELETE).build().perform();
    

    This will simulate pressing "delete" from your keyboard

    In case you have the element delete (as from your example) and you need to click it, you just perfomr the click action on the element.