How to press Ctrl+A to select all content in a page by Selenium WebDriver using Java

Viewed 98620

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?

4 Answers

Mac users should use Cmnd instead of Control:

element.sendKeys(Keys.chord(Keys.COMMAND, "a"));

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")
Related