Search code examples
javaseleniumwebdrivervirtual-keyboard

How to press Ctrl+0 (Zero) using Selenium WebDriver


I want to send the keys Ctrl and zero using Selenium WebDriver APIs

I tried the below code but not working

 Actions action = new Actions(driver);
        action.keyDown(Keys.CONTROL).sendKeys("F000").keyUp(Keys.CONTROL).perform();

Looking for help


Solution

  • Both these work for me:

    A nice WebDriver approach

    String ctrlZero = Keys.chord(Keys.CONTROL, "0");
    driver.findElement(By.tagName("html")).sendKeys(ctrlZero);
    

    and the pure Java approach working on a higher level:

    Robot r = new Robot();
    r.keyPress(KeyEvent.VK_CONTROL);
    r.keyPress(KeyEvent.VK_0);
    r.keyRelease(KeyEvent.VK_CONTROL);
    r.keyRelease(KeyEvent.VK_0);