[ruby] 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.

This question is related to ruby selenium selenium-webdriver keypress

The answer is


To click Ctrl+A, you can do it with Actions

  Actions action = new Actions(); 
  action.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0061')).perform();

\u0061 represents the character 'a'

\u0041 represents the character 'A'

To press other characters refer the unicode character table - http://unicode.org/charts/PDF/U0000.pdf


You could try this:

driver.findElement(By.xpath(id("anything")).sendKeys(Keys.CONTROL + "a");

This is what worked for me using C# (Visual Studio 2015) with Selenium:

new Actions(driver).SendKeys(Keys.Control + "A").Perform();

You can add as many keys as wanted using (+) in between.


For Python:

ActionChains(driver).key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform();

The simplest answer in C# (if you are C# inclined).

Actions action = new Actions(); 
action.KeyDown(OpenQA.Selenium.Keys.Control).SendKeys("a").KeyUp(OpenQA.Selenium.Keys.Control).perform();

This answer is almost given by Hari Reddy, but I have fixed the case which he'd got wrong on some keywords, added the KeyUp or you get in a mess leaving the control key down.

I've also added the clarification on OpenQA.Selenium.Keys, because you may also be using Windows.Forms on the same class as I was an require this clarity.

Lastly, I type "a" because I found that to be the simplest way and I can see no suggestion from the OP that they don't want the simplest answer.

Many thanks to Hari Reddy though as I was a novice in Actions class usage and I was writing many different commands. Chaining them together the way he showed is quicker :-)


Since Ctrl+A maps to ASCII code value 1 (Ctrl+B to 2, up to, Ctrl+Z to 26).

Try:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Remote;

namespace SeleniumHqTest
{
    class Test
    {
            IWebDriver driver = new InternetExplorerDriver();
            driver.Navigate().GoToUrl("http://localhost");
            IWebElement el = driver.FindElement(By.Id("an_element_id"));
            char c = '\u0001'; // ASCII code 1 for Ctrl-A
            el.SendKeys(Convert.ToString(c));
            driver.Quit();
    }
}

Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).build().perform();

It works for me:

OpenQA.Selenium.Interactions.Actions action 
    = new OpenQA.Selenium.Interactions.Actions(browser);
action.KeyDown(OpenQA.Selenium.Keys.Control)
    .SendKeys("a").KeyUp(OpenQA.Selenium.Keys.Control).Perform();

I found that in Ruby, you can pass two arguments to send_keys

Like this:

element.send_keys(:control, 'A')

By using the Robot class in Java:

import java.awt.Robot;
import java.awt.event.KeyEvent;

public class Test1
{
    public static void main(String[] args) throws Exception
    {
        WebDriver d1 = new FirefoxDriver();
        d1.navigate().to("https://www.youtube.com/");
        Thread.sleep(3000);
        Robot rb = new Robot();
        rb.keyPress(KeyEvent.VK_TAB);
        rb.keyRelease(KeyEvent.VK_TAB);
        rb.keyPress(KeyEvent.VK_TAB);
        rb.keyRelease(KeyEvent.VK_TAB);

        // Perform [Ctrl+A] Operation - it works
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_A);

        // It needs to release key after pressing
        rb.keyRelease(KeyEvent.VK_A);
        rb.keyRelease(KeyEvent.VK_CONTROL);
        Thread.sleep(3000);
    }
}


WebDriver driver = new FirefoxDriver();

Actions action = new Actions(driver); 

action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();

This method removes the extra call ( String.ValueOf() ) to convert unicode to string.


Java

The Robot class will work much more efficiently than sending the keys through Selenium sendkeys. Please try:

Example:

Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_A);

To use the above Robot class, you need to import java.awt.Robot;'.


In Selenium for C#, sending Keys.Control simply toggles the Control key's state: if it's up, then it becomes down; if it's down, then it becomes up. So to simulate pressing Control+A, send Keys.Control twice, once before sending "a" and then after.

For example, if we is an input IWebElement, the following statement will select all of its contents:

we.SendKeys(Keys.Control + "a" + Keys.Control);


Examples related to ruby

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew?

Examples related to selenium

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser Class has been compiled by a more recent version of the Java Environment How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? element not interactable exception in selenium web automation Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception?

Examples related to selenium-webdriver

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception? Scrolling to element using webdriver? Only local connections are allowed Chrome and Selenium webdriver Check if element is clickable in Selenium Java

Examples related to keypress

onKeyDown event not working on divs in React How can I listen for keypress event on the whole page? How to handle the `onKeyPress` event in ReactJS? detect key press in python? jQuery Keypress Arrow Keys Submit form on pressing Enter with AngularJS How to trigger an event in input text after I stop typing/writing? How to capture Enter key press? JQuery: detect change in input field Pressing Ctrl + A in Selenium WebDriver