简体   繁体   English

Selenium WebDriver拖放到滚动条

[英]Selenium WebDriver drag and drop to scrollbar

I have issue with Selenium WebDriver drag-and-drop. Selenium WebDriver拖放存在问题。 It didn't want drop to webelement in scroll-bar. 它不想放到滚动条中的webelement上。 I tried this: 我尝试了这个:

new Actions(SeleniumDriver.getDriver())).dragAndDrop(element, target).build().perform();

also tried by using offsets: 也尝试使用偏移量:

(new Actions(SeleniumDriver.getDriver()))
           .dragAndDropBy(element, xoffset, yoffset).build().perform();

and try use: 并尝试使用:

Actions builder = new Actions(SeleniumDriver.getDriver());
builder.clickAndHold(element).build().perform();
builder.moveToElement(target).build().perform();
builder.release(target).build().perform();

Any body know working solutions for scroll-bars? 任何机构都知道滚动条的有效解决方案吗? Thank's for any help. 谢谢你的帮助。

Make sure you are trying to drag the right element. 确保您尝试拖动正确的元素。 Maybe that element is not dragable, it could be an inner element that is dragable. 也许该元素不可拖动,它可能是可拖动的内部元素。

I found solution by using Java Robot class. 我通过使用Java Robot类找到了解决方案。

  1. Switch chrome to full screen: 将Chrome切换到全屏:

     DesiredCapabilities dc = new DesiredCapabilities(); ChromeOptions options = new ChromeOptions(); options.addArguments("--kiosk", "test-type"); dc.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new ChromeDriver(dc); 
  2. Get coordinates of start and destination element: 获取起始元素和目标元素的坐标:

     // start coordinates int startX = new Integer(element.getLocation().x); int startY = new Integer(element.getLocation().y); // destination dimensions int startWidth = new Integer(element.getSize().width); int startHeight = new Integer(element.getSize().height); // destination coordinates int destinationX = new Integer(target.getLocation().x); int destinationY = new Integer(target.getLocation().y); // destination dimensions int destinationWidth = new Integer(target.getSize().width); int destinationHeight = new Integer(target.getSize().height); // work out destination coordinates int endX = Math.round(destinationX + (destinationWidth / 2)); int endY = Math.round(destinationY + (destinationHeight / 2)); int sX = Math.round(startX + (startWidth / 2)); int sY = Math.round(startY + (startHeight / 2)); 
  3. Use Java Robot class for drag and drop: 使用Java Robot类进行拖放:

     Thread.sleep(1000); Robot robot = new Robot(); robot.mouseMove(sX, sY); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseMove(endX, endY); robot.mouseRelease(InputEvent.BUTTON1_MASK); 

A few ideas; 一些想法;

  • A small sleep is required after the click and hold. 单击并按住后需要小睡。 I have tested applications which required 100ms of hold until the drag and drop us initialized. 我已经测试了需要100毫秒保持时间的应用程序,直到初始化拖放操作为止。

  • It could be using HTML 5 drag and drop which last I tried wasn't supported by Selenium Selenium可能不支持HTML 5拖放功能,而我上次尝试过

  • wrong element as suggested by user3723314 用户3723314建议的元素错误

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

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