我试图双击一个元素,但无法执行该操作。单击一次可以在同一元素上正常工作。我想念什么吗?有人可以帮我吗?
元素的HTML:
<div class="container">
<div class="row">
<div class="btn-group-vertical col-12">
<a href="3overview.php" class="btn btn-primary">Overview</a>
<a href="" class="btn btn-primary">Button</a>
<a href="" class="btn btn-primary">Button</a>
<a href="" class="btn btn-primary">Button</a>
<br>
<a href="" class="btn btn-warning">Button</a>
<a href="" class="btn btn-danger">Button</a>
</div>
</div>
</div>
我尝试了多种双击元素的方法:
<tbody><tr class="mclS" tabindex="0"> <td><div class="mclC" style="height:14px;"> * Quarter to Date</div></td> </tr> </tbody>
答案 0 :(得分:0)
对于reference。
您需要对元素执行以下操作:
Actions action = new Actions(driver);
WebElement date = driver.findElement(By.cssSelector(".mlstBody>tbody>tr:nth-child(8)"));
action.doubleClick(date).perform();
注意::此示例使用Java。
其他说明:对于Selenium 3.5及更高版本,您需要执行以下操作:
action.moveToElement(driver.findElement(By.cssSelector(".msltBody>tbody>tr:nth-child(8)")).doubleClick().build().perform();
答案 1 :(得分:0)
您似乎很接近。要通过doubleClick()
类调用Actions
,可以使用以下解决方案之一:
使用 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();
使用 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();
由于您仍然无法在所需元素上调用doubleClick()
作为进行鼠标双击的替代方法,因此您可以编写脚本并将其传递给executeScript()
方法如下:
脚本:
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)); " +
"} " ;
通过executeScript()
中的@Test
调用脚本:
new Actions(driver).moveToElement(myElem, posX, posY).perform();
((JavascriptExecutor)driver).executeScript(jsDoubleClick, myElem, posX, posY);
答案 2 :(得分:0)
我猜你正在使用Firefox?我认为在用doubleclick和geckodriver编写问题。我认为还没有解决。我看到您在JavaScript中尝试了一种方法。你可以这样尝试吗?它在Firefox中对我有用。
document.querySelector(".mlstBody>tbody>tr:nth-child(8)").dispatchEvent(new MouseEvent("dblclick"));