首页 > 解决方案 > Winapp 驱动程序中如何进行拖放。

问题描述

我正在使用 Outlook,我需要将一封电子邮件拖放到第二个文件夹中。

我厌倦了下面的代码:

Actions builder = new Actions(driver);
builder.ClickAndHold(from).MoveToElement(to).Release().Build().Perform();
Or
builder.DragAndDrop(from, to).MoveToElement(to).Build().Perform();

有了这个,我可以执行单击并按住鼠标操作,但电子邮件不会被移动到第二个文件夹中。

标签: c#visual-studio-2019winappdriver

解决方案


下一个为我工作的 IWebElement DragAndDrop扩展

public static void DragAndDrop(this IWebElement source, IWebElement destination)
{
    var destinationCenterX = destination.Location.X + destination.Size.Width / 2;
    var destinationCenterY = destination.Location.Y + destination.Size.Height / 2;
    var action = new Actions(source.GetDriver());
    action.MoveToElement(source).Build().Perform();
    action.ClickAndHold(source).MoveByOffset(destinationCenterX, destinationCenterY).Build().Perform();
    destination.Click();
    action.Release().Perform();
}

推荐阅读