繁体   English   中英

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

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

我需要使用 Selenium WebDriver(java) 按下控制+鼠标单击键。 我需要在我的脚本中使用 select 多个元素。 有什么办法吗?

我检查了 Selenium 库,发现 selenium 只允许按下特殊键和功能键。

您可以使用WebDriver中已编写的库操作。

简要说明发生的事情:

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

在这种情况下,您可以根据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();

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

使用jquery代码实现相同的功能

JavascriptExecutor js = (JavascriptExecutor) driver;  
String script = "e = jQuery.Event('click');e.ctrlKey = true;    $('secondRow_Css_locator').trigger(e);";
js.executeScript(script);

或者你也可以使用机器人类,但它可以锁定你的屏幕片刻

 Robot robot = new Robot();
 robot.keyPress(KeyEvent.VK_CONTROL);
 robot.keyRelease(KeyEvent.VK_CONTROL);
 robot.mousePress(InputEvent.BUTTON1_MASK);
 robot.mouseRelease(InputEvent.BUTTON1_MASK);

截至2018年,这是第一个弹出的结果。 之前它在FF 61之后工作正常(直接跳转形式47到61)它开始打破。 不幸的是,没有一个答案对我有用。 通过action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform();解决它action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform(); 只是逐个迭代每个元素

在使用Mac的情况下,te代码将是下一个:

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

它与我一起使用:

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