我使用下面的拖放代码将图片拖到光场中。这在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..");
}
这就是我现在在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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句