Ripon Al Wasim Ripon Al Wasim - 1 year ago 182
Java Question

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

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

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

Answer Source

To Select 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"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download