简体   繁体   English

双击操作不起作用,而单击对硒中的元素起作用

[英]Double click action not working whereas single click works on an element in selenium

I am trying to double click on an element but unable to perform the action. 我试图双击一个元素,但无法执行该操作。 Single click works fine on the same element. 单击一次可以在同一元素上正常工作。 Am i missing something?. 我想念什么吗? Can someone please help me out with this. 有人可以帮我这个忙。

HTML of the element: 元素的HTML:

<tbody><tr class="mclS" tabindex="0"> <td><div class="mclC" style="height:14px;">&nbsp;&nbsp;*&nbsp;Quarter&nbsp;to&nbsp;Date</div></td> </tr> </tbody>

I have tried various ways of double clicking the element: 我尝试了多种双击元素的方法:

WebElement date = driver.findElement(By.cssSelector(".mlstBody>tbody>tr:nth-child(8)"));

=> actions.doubleClick(date).build().perform();

=> actions.doubleClick(date);

=> ((JavascriptExecutor)driver).executeScript("var evt = document.createEvent('MouseEvents');" + "evt.initMouseEvent('dblclick',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);" + "arguments[0].dispatchEvent(evt);",date);

=> actions.moveToElement(date).doubleClick().build();
actions.perform();

For reference . 参考

You need to perform the action on the element like this: 您需要对元素执行以下操作:

Actions action = new Actions(driver);
WebElement date = driver.findElement(By.cssSelector(".mlstBody>tbody>tr:nth-child(8)"));
action.doubleClick(date).perform();

NOTE: This example uses Java. 注意:此示例使用Java。

Additional NOTE: For Selenium 3.5 and up, you will need to do the following: 附加说明:对于Selenium 3.5及更高版本,您需要执行以下操作:

action.moveToElement(driver.findElement(By.cssSelector(".msltBody>tbody>tr:nth-child(8)")).doubleClick().build().perform();

Seems you were pretty close. 看来您已经很接近了。 To invoke doubleClick() through Actions class you can use either of the following solutions: 要通过Actions类调用doubleClick() ,可以使用以下解决方案之一:

  • Using cssSelector : 使用cssSelector

     WebElement date = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("tr.mclS>td>div.mclC"))); new Actions(driver).moveToElement(date).doubleClick().build().perform(); 
  • Using xpath : 使用xpath

     WebElement date = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//tr[@class='mclS']/td/div[@class='mclC' and contains(.,'Date')]"))); new Actions(driver).moveToElement(date).doubleClick().build().perform(); 

Update 更新

As you are still unable to invoke doubleClick() on the desired element as an alternative to make Mouse Double Click you can write a script and pass it to the executeScript() method as follows: 由于仍然无法在所需元素上调用doubleClick()作为鼠标双击的替代方法,因此您可以编写脚本并将其传递给executeScript()方法,如下所示:

  • Script : 剧本:

     String jsDoubleClick = "var target = arguments[0]; " + "var offsetX = arguments[1]; " + "var offsetY = arguments[2]; " + "var rect = target.getBoundingClientRect(); " + "var cx = rect.left + (offsetX || (rect.width / 2)); " + "var cy = rect.top + (offsetY || (rect.height / 2)); " + " " + "emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " + "emit('mouseup', {clientX: cx, clientY: cy}); " + "emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " + "emit('mouseup', {clientX: cx, clientY: cy}); " + "emit('click', {clientX: cx, clientY: cy, detail: 2}); " + " " + "function emit(name, init) { " + "target.dispatchEvent(new MouseEvent(name, init)); " + "} " ; 
  • Invoking the script through executeScript() from your @Test : 通过@Test executeScript()调用脚本:

     new Actions(driver).moveToElement(myElem, posX, posY).perform(); ((JavascriptExecutor)driver).executeScript(jsDoubleClick, myElem, posX, posY); 

I'm going to guess you are using Firefox? 我猜你正在使用Firefox? I think there is an issue written up with doubleclick and the geckodriver. 我认为在用doubleclick和geckodriver编写问题。 I don't think it's fixed yet. 我认为还没有解决。 I see you tried one way in JavaScript. 我看到您在JavaScript中尝试了一种方法。 Can you try this way though? 你可以这样尝试吗? It worked for me in Firefox. 它在Firefox中对我有用。

document.querySelector(".mlstBody>tbody>tr:nth-child(8)").dispatchEvent(new MouseEvent("dblclick"));

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

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