簡體   English   中英

無法DoubleClick Selenium Java

[英]Not able to doubleClick Selenium Java

我是自動化測試的初學者。 我已經安裝了Eclipse作為執行自動化任務的IDE,而我使用的語言是Java。 我的網絡應用程序中有一個模塊,其中我需要非常快速地雙擊Web元素。 第一次點擊和第二次點擊之間的時間差應小於半秒。

我寫了以下代碼:

Actions actions = new Actions(driver);
actions.doubleClick(driver.findElement(By.xpath("//div[contains(text(), 'Sonam')]"))).doubleClick().build().perform();

我究竟做錯了什么?

您應該先將鼠標移到元素上。

嘗試這個

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//div[contains(text(), 'Sonam')]"))).doubleClick().perform();

要不就:

 action.doubleClick(driver.findElement(By.xpath("//div[contains(text(), 'Sonam')]"))).perform();

使用Actions類,您可以對元素執行DoubleClick。

干得好:

Actions action = new Actions(driver); 
action.doubleClick(driver.findElement(By.xpath("//div[contains(text(), 'Sonam')]"))).build().perform();

要么

您可以使用JavaScriptExecutor使用Javascript雙擊,如下所示:

String jsCodeToDblClick = "var targElement=arguments[0]; var clEvent=document.createEvent('MouseEvents'); clEvent.initEvent('dblclick', true, true');targElement.dispatchEvent (clEvent);";

((JavascriptExecutor)driver).executeAsyncScript(jsCodeToDblClick ,driver.findElement(By.xpath("//div[contains(text(), 'Sonam')]")));

這有效!!!

您可以嘗試以下操作:

WebElement ele1 = driver.findElement(By.xpath("//div[contains(text(), 'Sonam')]")));
Actions actions = new Actions(driver);
actions.doubleClick(ele1).build().perform();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM