使用 Web 驱动程序双击网格中的记录的 Selenium webdriver Java 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21907007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:21:41  来源:igfitidea点击:

Selenium webdriver Java code using web driver for double click a record in a grid

javaseleniumselenium-webdriver

提问by Gokul

How to write selenium java code for doubleClick()on a record using web driver?

如何doubleClick()使用 Web 驱动程序为记录编写 selenium java 代码?

I have displayed some records in the body part. Once I clicked on a record we should get a popup window to update it.

我在正文部分显示了一些记录。一旦我点击了一条记录,我们应该会得到一个弹出窗口来更新它。

Please suggest how to write Selenium Java code using web driver.

请建议如何使用 Web 驱动程序编写 Selenium Java 代码。

I have tried following code:

我试过以下代码:

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().build().perform();

采纳答案by Gokul

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().perform();

This code works!!!

这段代码有效!!!

回答by Mark Rowlands

You should use the Actions()class as this includes a 'double-click' action.

您应该使用Actions()该类,因为它包含“双击”操作。

Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Test"))).doubleClick().build().perform();

回答by Santoshsarma

Use Actionsclass to perform mouse, keyboard actions on WebElements using WebDriver.

使用Actions类通过 WebDriver 在 WebElements 上执行鼠标、键盘操作。

Actions action = new Actions(driver);
WebElement element=driver.findElement(By.linkText("TEST"));

//Double click
action.doubleClick(element).perform();

//Mouse over
action.moveToElement(element).perform();

//Right Click
action.contextClick(element).perform();

回答by Hymanin Shah

You can make use of Actionsclass of WebDriver to perform composite actions like Double click, Drag and Drop, Hoveretc.

您可以使用WebDriver的Actions类来执行复合操作,例如 双击、拖放、悬停等。

// Creates an instance of Actions class, passing current driver instance.

// 创建 Actions 类的实例,传递当前驱动程序实例。

Actions builder = new Actions(driver);

Way 1:

方式一:

// Gets an Action class object that holds an action/ a set of actions

// 获取一个 Action 类对象,该对象包含一个动作/一组动作

Action action = builder.doubleClick(element);

// Builds the set of actions/ single action using build() and executes on browser using perform() method.

// 使用 build() 构建一组动作/单个动作,并使用 perform() 方法在浏览器上执行。

action.build().perform();

Way 2:

方式二:

// Calls build() and perform() methods directly on Actions class instance

// 直接在 Actions 类实例上调用 build() 和 perform() 方法

builder.doubleClick().build().perform();

回答by Mykhailo Kovalevskyi

And in case if have no additional actions binded to singleclick, you can use:

如果没有绑定到单击的附加操作,您可以使用:

driver.findElement(By.xpath("%youXPath%"))).click;
driver.findElement(By.xpath("%youXPath%"))).click;

Actually, it should works in most cases (except you have some custom system doubleclick settings)

实际上,它应该适用于大多数情况(除非您有一些自定义系统双击设置)

回答by Ran Adler

  WebElement element = driver.findElement(selector);
  Actions builder = new Actions(driver);
  builder.doubleClick(element).perform();

回答by Ron Patrick Horton

I implemented Ran's (immediately above my post) solution. I'm writing Java in Eclipse and using Selenium WebDriver.

我实施了 Ran 的(就在我的帖子上方)解决方案。我正在 Eclipse 中编写 Java 并使用 Selenium WebDriver。

There are 2 imports that you'll need:

您需要 2 个导入:

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

Then, I implemented the code thusly:

然后,我这样实现了代码:

WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[1]/div[3]/div[8]/div[2]/div/div[2]/div/table/tbody/tr[2]"));
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();

Thanks to Ran! I'd been struggling with this one for several hours. Invoking the single click twice doesn't work for me - too much time between the events to be captured by the browser under test as a double-click.

感谢冉!我一直在与这个斗争了几个小时。两次调用单击对我不起作用 - 被测试浏览器作为双击捕获的事件之间的时间太长。

回答by iamsankalp89

Try this code:

试试这个代码:

Actions action = new Actions(driver);
WebElement btnElement=driver.findElement("Locator of element"));
action.doubleClick(btnElement).build().perform();