Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 324
Ripon Al Wasim Avatar asked Jul 20 '12 11:07

Ripon Al Wasim


People also ask

How do I select CTRL in Selenium?

We can also perform a CTRL+A press by simply using the sendKeys() method. We have to pass Keys. CONTROL along with the string A by concatenating with +, as an argument to the method.

How do you select all text in Selenium?

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

How do you select all text and clear in Selenium?

Select all text in a textbox Selenium RC using Ctrl + A.


1 Answers

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")); 
like image 71
Nazeer Mohammed Avatar answered Sep 25 '22 22:09

Nazeer Mohammed