【问题标题】:How to drag using Selenium webdriver 3 where the underlying element to drag from is an <img> tag如何使用 Selenium webdriver 3 进行拖动,其中要拖动的基础元素是 <img> 标记
【发布时间】:2017-06-01 16:52:54
【问题描述】:

我将 Selenium 3 与 Java、Cucumber 和 ChromeDriver 一起使用。我正在尝试从 img 元素中拖动,但 Selenium 不允许将焦点转移到该元素以进行拖放。我已经尝试了以下所有选项。请。让我知道是否有人可以提供帮助。

使用键盘操作:

Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL).click(someElement).click(someOtherElement).        keyUp(Keys.CONTROL).build().perform();

使用鼠标操作:

Actions builder = new Actions(driver);
builder.clickAndHold(someElement).moveToElement(otherElement).release( otherElement).build().perform();

代码适用于机器人类:

public String dragElementFrom="//div[@class='asset-instructions col-xs-6']/div[5]/div/ul/li[1]/div/img";
public String dragElementTo="//*[@id='parity-on-page-preview']";

WebElement e=objHelp.WaitForElement(driver,By.xpath(obj.dragElementFrom)); WebElement f=objHelp.WaitForElement(driver,By.xpath(obj.dragElementTo));

 Point coordinates1 = e.getLocation();
 Point coordinates2 = f.getLocation();
 Robot robot = new Robot();

 robot.mouseMove(coordinates1.getX()+55, coordinates1.getY()+118);
 robot.mousePress(InputEvent.BUTTON1_MASK);
 robot.mouseMove(coordinates2.getX()+200, coordinates2.getY()+200);
 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 Thread.sleep(2000);

机器人类的问题是当我运行测试时,我需要确保浏览器窗口聚焦,否则机器人类不起作用。

我也尝试过使用 Firefox,但还是一样。我也研究了stackoverflow,但在任何地方都找不到我的答案。

【问题讨论】:

    标签: java selenium cucumber


    【解决方案1】:

    如果我理解正确,Selenium 3 不支持 HTML 5 拖放。他们正在努力:https://github.com/SeleniumHQ/selenium/issues/2285 有基于 javascript 的解决方案,但它们并不总是有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2012-08-07
      相关资源
      最近更新 更多