24

I want to select all content by pressing Ctrl+a from keyboard by using WebDriver with Java. I wrote the following code:

Actions actionObj = new Actions(driver);
actionObj.keyDown(Keys.CONTROL)
         .sendKeys(Keys.chord("A"))
         .keyUp(Keys.CONTROL)
         .perform();

Unfortunately, it did not work. What's the wrong in my WebDriver Java code?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176

4 Answers4

39

To select the whole page:

driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "a"));

cssSelector is faster than xpath. So it could be done by using CSSPath also. Below is the way:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "a"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nazeer Mohammed
  • 406
  • 4
  • 2
17

Try to chord the Ctrl+A keys. The code below is working in my case:

element.sendKeys(Keys.chord(Keys.CONTROL, "a"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luiz Fernando Penkal
  • 1,011
  • 1
  • 7
  • 8
  • yes, this is working nicely. I visited www.google.com and I selected the text of google text box as: driver.findElement(By.id("gbqfq")).sendKeys(Keys.chord(Keys.CONTROL, "a")); It is working well according to your direction... thanks. I want to select whole page of google. How can I do that? – Ripon Al Wasim Jul 23 '12 at 05:07
  • I stumbled upon the answer to this due to a bug in some of my automated tests... I used a CSS selector that ended up selecting an element of the page that wasn't an input field (like a div, for example)... when the test sent the CTRL+A to this element it ended up selecting the whole page. – Luiz Fernando Penkal Jul 25 '12 at 15:24
  • Complementing the comment above, Webdriver generally tries to emulate the user in the best way it can... What happened when it sent a CTRL+A to an element that wasn't an input is what would happen if we did it manually (clicked anywhere in the page that wasn't an input and pressed CTRL+A). – Luiz Fernando Penkal Jul 25 '12 at 15:27
  • Thanks a lot. I sent CTRL+A to a div of the page and whole page was selected. My main goal was to select all. This is good solution Luiz – Ripon Al Wasim Jul 26 '12 at 03:22
  • I get `AttributeError: type object 'Keys' has no attribute 'chord'` error for some reason when I use chord. Any ideas folks? – Uthman Jul 04 '15 at 22:35
  • What is the alternate in C#? – Ashok kumar Ganesan Apr 08 '20 at 09:45
1

Mac users should use Cmnd instead of Control:

element.sendKeys(Keys.chord(Keys.COMMAND, "a"));
Raufray
  • 11
  • 2
1

Python

#! /usr/bin/env python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.service import Service

service_obj = Service(executable_path="C:/Firefox-Webdriver/geckodriver.exe")
browser = webdriver.Firefox(service=service_obj)

browser.get('http://localhost:8000/')
browser.get(url)

body = browser.find_element(By.TAG_NAME, "body")
body.send_keys(Keys.CONTROL + "a")
body.send_keys(Keys.CONTROL + "c")
Jason
  • 813
  • 11
  • 13