Search code examples
rubyseleniumselenium-webdriverkeypress

Pressing Ctrl + A in Selenium WebDriver


Is there a way to press the Ctrl + A keys using Selenium WebDriver?

I checked the Selenium libraries and found that Selenium allows key press of special and function keys only.


Solution

  • One more solution (in Java, because you didn't tell us your language - but it works the same way in all languages with Keys class):

    String selectAll = Keys.chord(Keys.CONTROL, "a");
    driver.findElement(By.whatever("anything")).sendKeys(selectAll);
    

    You can use this to select the whole text in an <input>, or on the whole page (just find the html element and send this to it).


    For using Selenium Ruby bindings:

    There's no chord() method in the Keys class in Ruby bindings. Therefore, as suggested by Hari Reddy, you'll have to use Selenium Advanced user interactions API, see ActionBuilder:

        driver.action.key_down(:control)
                     .send_keys("a")
                     .key_up(:control)
                     .perform