问题描述
以下代码适用于 Firefox,但不适用于 Chrome.从我在网上找到的内容来看,Actions.DragAndDrop 似乎不适用于 Chrome.我正在尝试移动 SVG 元素.
The below code works in Firefox but not in Chrome. From what I could find online it seems that Actions.DragAndDrop does not work with Chrome. I am trying to move SVG elements.
有其他选择吗?
var action = new Actions(driver);
action
.DragAndDropToOffset(middle, 100, 100)
.Build()
.Perform();
Selenium.Webdriver: v3.141.0
Selenium.Webdriver: v3.141.0
Selenium.Webdriver.ChromeDriver: v76.0.3809.68
Selenium.Webdriver.ChromeDriver: v76.0.3809.68
Selenium.Firefox.Webdriver: v0.24.0
Selenium.Firefox.Webdriver: v0.24.0
Chrome:版本 76.0.3809.100(官方版本)(64 位)
Chrome: Version 76.0.3809.100 (Official Build) (64-bit)
推荐答案
这是我在 Chrome 中使用的.请记住,在使用拖放功能时,您需要一个起始元素来单击,以及将要放置它的第二个元素.
This is what I use in Chrome. Remember when using drag and drop you need a starting element to click, and a second element where you are going to drop it.
调用:
var ele1 = Driver.FindElement(By.Xpath("//button[@class='cz2__images__image-content cz2__images--draggable']"));
var ele2 = Driver.FindElement(By.Xpath("//button[@class='Destination']"));
DragAndDrop(ele1, ele2);
方法:
public static void DragAndDrop(IWebElement element1, IWebElement element2)
{
WaitForElementEnabled(element1);
WaitForElementEnabled(element2);
var builder = new Actions(Driver);
var dragAndDrop = builder.ClickAndHold(element1).MoveToElement(element2).Release(element1).Build();
dragAndDrop.Perform();
}
或....
public static void test ()
{
var test1 = _webDriver.FindElement(By.Id("myid"));
var test2 = _webDriver.FindElement(By.Id("myid2"));
Actions builder1 = new Actions(_webDriver);
IAction dragAndDrop1 = builder1.ClickAndHold(test1).MoveToElement(test2).Release(test1).Build();
dragAndDrop1.Perform();
}
这篇关于Selenium:如何使用 C# 在 Chrome 中拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!