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

爱⌒轻易说出口 提交于 2019-12-17 12:16:30

问题


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?


回答1:


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



回答2:


Have you tried to chord the Ctrl+A keys? The code below is working in my case:

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


来源:https://stackoverflow.com/questions/11578768/how-to-press-ctrla-to-select-all-content-in-a-page-by-selenium-webdriver-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!