简体   繁体   English

使用 java 在 Selenium WebDriver 中按下(Ctrl + 鼠标单击)

[英]Key press in (Ctrl + mouse click) in Selenium WebDriver using java

I need to press control+mouse click keys using Selenium WebDriver(java).我需要使用 Selenium WebDriver(java) 按下控制+鼠标单击键。 I need to select multiple element in my script.我需要在我的脚本中使用 select 多个元素。 Is there any way to do it?有什么办法吗?

I checked the Selenium libraries and found that selenium allows key press of special and functional keys only.我检查了 Selenium 库,发现 selenium 只允许按下特殊键和功能键。

There is already written library Actions in WebDriver which you can use. 您可以使用WebDriver中已编写的库操作。

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. 首先,您按下控制按钮,然后在定义的WebElemen对象上单击(在这种情况下)3次)然后您将取消控制并完成操作。

In this case you can achive the selection of 3 items (or opening a 3 new tabs) depending on what your WebElements are. 在这种情况下,您可以根据WebElements的内容选择3个项目(或打开3个新选项卡)。

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

Do it with the help of 'Actions' as below: 在'Actions'的帮助下完成,如下所示:

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

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

As of 2018 the is the first results pops up. 截至2018年,这是第一个弹出的结果。 Earlier it was working fine after FF 61 (direct jump form 47 to 61) It starts breaking. 之前它在FF 61之后工作正常(直接跳转形式47到61)它开始打破。 unfortunately none of the answer worked for me. 不幸的是,没有一个答案对我有用。 Solved it by action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform(); 通过action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform();解决它action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform(); just iterating every element one by one 只是逐个迭代每个元素

In the case of using Mac, te code would be next: 在使用Mac的情况下,te代码将是下一个:

action.keyDown(Keys.COMMAND)
            .click(WebElement)
            .keyUp(Keys.COMMAND)
            .build()
            .perform();

it worked with me using this:它与我一起使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM