Selenium拖放在Chrome或Firefox中无效

Pit*_*k76 3 c# selenium selenium-webdriver

我使用下面的拖放代码将图片拖入照片场.这在InternetExplorer中工作正常,但在Firefox或Chrome中不起作用.我不明白为什么不.

正如您在下面的代码中看到的,我尝试了很多不同的方法来执行拖放操作,但它们都不起作用.主要问题是在释放图像后目标未更新.我看到丢弃发生但没有更新.

有谁知道为什么会这样?我正在使用C#和最新的Selenium驱动程序2.39,Chrome驱动程序2.8.

public static void DoDragAndDrop(IWebDriver driver, string dragImageId, string dropFieldId)
    {
        Console.WriteLine("Drag and drop image '{0}' to the editor {1}..", dragImageId, dropFieldId);
        IWebElement dragElement = WebDriverExtensions.TryFindElement(By.Id(dragImageId));
        IWebElement dropElement = WebDriverExtensions.TryFindElement(By.Id(dropFieldId));

        if(dragElement == null)
            Console.WriteLine("dragElement is null");
        if(dropElement == null)
            Console.WriteLine("dropElement is null");

        ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView();", dragElement);
        Thread.Sleep(500);

        ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView();", dropElement);
        Thread.Sleep(200);

        Console.WriteLine("Drag and drop 1");
        var builder1 = new Actions(driver);
        builder1.MoveToElement(dragElement).ClickAndHold();
        builder1.MoveToElement(dropElement).Build().Perform();
        Thread.Sleep(2000);

        Console.WriteLine("Drag and drop 2");
        var builder2 = new Actions(driver);
        builder2.DragAndDrop(dragElement, dropElement);
        Thread.Sleep(2000);

        Console.WriteLine("Drag and drop 3");
        var builder3 = new Actions(driver);
        builder3.DragAndDrop(dragElement, dropElement).Build().Perform();
        IAction dragAndDrop = builder3.ClickAndHold(dragElement)
            .MoveToElement(dropElement)
            .Release(dropElement)
            .Build();
        dragAndDrop.Perform();
        Thread.Sleep(2000);


        Thread.Sleep(1000);
        Console.WriteLine("Drag and drop succeeded..");
    }
Run Code Online (Sandbox Code Playgroud)

Pit*_*k76 6

这就是我现在在FireFox中工作的方式.Chrome仍然失败.唯一的区别是我在MoveToElement方法中添加了偏移量,如The Rookies注释中所示.

var builder = new Actions(driver);
        builder.ClickAndHold(dragElement);
        builder.MoveToElement(dropElement, 5, 5);
        builder.Perform();
        Thread.Sleep(250);
        builder.Release(dropElement);
        builder.Perform();
Run Code Online (Sandbox Code Playgroud)