How to press Ctrl+A to select all content in a page by Selenium WebDriver using Java
-
22-06-2021 - |
質問
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?
解決
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"));
他のヒント
Try to chord the Ctrl+A keys. The code below is working in my case:
element.sendKeys(Keys.chord(Keys.CONTROL, "a"));
Mac users should use Cmnd instead of Control:
element.sendKeys(Keys.chord(Keys.COMMAND, "a"));
所属していません StackOverflow