Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
AJJ Avatar asked Jul 16 '12 11:07

AJJ


People also ask

How do I use CTRL S in Selenium?

Press SHIFT - using keyDown. Press Ctrl - using keyDown. Press then release S (this key can be pressed and immediately released using sendKeys method)


4 Answers

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
like image 173
Petr Janeček Avatar answered Oct 20 '22 20:10

Petr Janeček


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

like image 20
Hari Reddy Avatar answered Oct 20 '22 20:10

Hari Reddy


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);

like image 13
Robert P Avatar answered Oct 20 '22 21:10

Robert P


You could try this:

driver.findElement(By.xpath(id("anything")).sendKeys(Keys.CONTROL + "a");
like image 5
Harish Gautham Avatar answered Oct 20 '22 20:10

Harish Gautham