【发布时间】:2014-01-21 09:38:36
【问题描述】:
我使用下面的拖放代码将图片拖放到照片字段中。这在 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..");
}
【问题讨论】:
标签: c# selenium selenium-webdriver