我使用下面的拖放代码将图片拖入照片场。这在InternetExplorer中运行正常,但在Firefox或Chrome中不起作用。 我不明白为什么不。Selenium在Chrome或Firefox中拖放不起作用
正如你在下面的代码中看到的,我尝试了很多不同的方式来进行拖放操作,但是它们都不起作用。 主要问题是释放图像后目标未更新。 我看到下降发生,但没有更新。
有没有人有任何想法,为什么这是?我使用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..");
}
这个固定为我在Chrome v55.0.2。出于某种原因,元素现在消失,但至少会拖拽元素。 – KthProg
一旦我从builder.release()中删除元素,一切都很完美。 – KthProg