使用 java 在 Selenium WebDriver 中按下按键(Ctrl + 鼠标单击)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32627981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Key press in (Ctrl + mouse click) in Selenium WebDriver using java
提问by Hetal Patel
I need to press control+mouse click keys using Selenium WebDriver(java). I need to select multiple element in my script. Is there any way to do it?
我需要使用 Selenium WebDriver(java) 按下 control+mouse click 键。我需要在我的脚本中选择多个元素。有什么办法吗?
I checked the Selenium libraries and found that selenium allows key press of special and functional keys only.
我检查了 Selenium 库,发现 selenium 只允许按下特殊键和功能键。
回答by Shubham Jain
You achieve same using jquery code
您使用 jquery 代码实现相同的目标
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "e = jQuery.Event('click');e.ctrlKey = true; $('secondRow_Css_locator').trigger(e);";
js.executeScript(script);
OR you can also use robot class but it can lock your screen for a moment
或者您也可以使用机器人课程,但它可以暂时锁定您的屏幕
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
回答by Arsey
There is already written library Actions in WebDriver which you can use.
您可以使用 WebDriver 中已经编写的库 Actions。
Short Description of what is happening:
正在发生的事情的简短描述:
First you are pressing the Control button and then you are clicking (in this case) 3 times on your defined WebElemen objects) then your are unpressing the Control and finish your Actions.
首先,您按下 Control 按钮,然后在您定义的 WebElemen 对象上单击(在本例中)3 次),然后您松开 Control 并完成您的操作。
In this case you can achive the selection of 3 items (or opening a 3 new tabs) depending on what your WebElements are.
在这种情况下,您可以选择 3 个项目(或打开 3 个新选项卡),具体取决于您的 WebElements 是什么。
Actions actions = new Actions(driver);
actions.keyDown(Keys.LEFT_CONTROL)
.click(first_WebElement)
.click(second_WebElement)
.click(third_WebElement)
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();
回答by Deepak Arockiaraj
Do it with the help of 'Actions' as below:
在“操作”的帮助下进行,如下所示:
Actions action=new Actions(driver);
action.keyDown(Keys.CONTROL).build().perform();
driver.findElement(By.xpath(".//*[@id='selectable']/li[1]")).click();
driver.findElement(By.xpath(".//*[@id='selectable']/li[3]")).click();
action.keyUp(Keys.CONTROL).build().perform();
回答by Pranoy Sarkar
As of 2018 the is the first results pops up. Earlier it was working fine after FF 61 (direct jump form 47 to 61) It starts breaking. unfortunately none of the answer worked for me. Solved it by action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform();
just iterating every element one by one
截至 2018 年,这是第一个弹出的结果。早些时候它在 FF 61 之后工作正常(从 47 到 61 的直接跳转)它开始坏了。不幸的是,没有一个答案对我有用。action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform();
只需一一迭代每个元素即可解决
回答by nosequeweaponer
In the case of using Mac, te code would be next:
在使用 Mac 的情况下,接下来是 te 代码:
action.keyDown(Keys.COMMAND)
.click(WebElement)
.keyUp(Keys.COMMAND)
.build()
.perform();