在 Selenium WebDriver 中按 Ctrl + A [英] Pressing Ctrl + A in Selenium WebDriver
问题描述
有没有办法使用 Selenium WebDriver 按下 Ctrl + A 键?
Is there a way to press the Ctrl + A keys using Selenium WebDriver?
我检查了 Selenium 库,发现 Selenium 允许按特殊键和 仅功能键.
I checked the Selenium libraries and found that Selenium allows key press of special and function keys only.
推荐答案
另一种解决方案(在 Java 中,因为您没有告诉我们您的语言 - 但它在使用 Keys代码>类):
One more solution (in Java, because you didn't tell us your language - but it works the same way in all languages with Keys
class):
String selectAll = Keys.chord(Keys.CONTROL, "a");
driver.findElement(By.whatever("anything")).sendKeys(selectAll);
您可以使用它来选择 或整个页面上的整个文本(只需找到
html
元素并将其发送给它).
You can use this to select the whole text in an <input>
, or on the whole page (just find the html
element and send this to it).
用于使用 Selenium Ruby 绑定:
For using Selenium Ruby bindings:
Keys
类 Ruby 绑定.因此,正如 Hari Reddy 所建议的,您必须使用 Selenium Advanced 用户交互 API,请参阅 ActionBuilder
:
There's no chord()
method in the Keys
class in Ruby bindings. Therefore, as suggested by Hari Reddy, you'll have to use Selenium Advanced user interactions API, see ActionBuilder
:
driver.action.key_down(:control)
.send_keys("a")
.key_up(:control)
.perform
这篇关于在 Selenium WebDriver 中按 Ctrl + A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!