使用Selenium和C#进行draggable = true时,拖放操作无法使用操作 [英] Drag and drop not working using Actions when draggable=true using Selenium and C#
问题描述
可以正确识别元素,我可以看到鼠标在这两个元素之间移动,但是没有发生拖放.单击并按住时,UI不会显示任何突出显示.也没有错误.
Elements are identified correctly and i can see mouse moving between this two elements but drag and drop not happening. Ui not displayed any highlights when click and hold. No errors also.
我尝试了在不同讨论中提出的不同解决方案,但没有一个对我有用
I have tried different solutions suggested on different discussions none of them working for me
我的代码
_actions = new Actions(Driver.WebDriver);
var dragAndDrop = _actions.ClickAndHold(parentRow)
.MoveToElement(childRow )
.Release(target)
.Build();
dragAndDrop.Perform();
Driver.Wait();
这就是我识别元素的方式
This is how i am identifying elements
var childList =Driver.WebDriver.FindElements(By.ClassName("itl-treeNode-title"));
var parentRow = childList.FirstOrDefault(x => x.Text.Equals(parentSrc)).FindElement(By.XPath("following-sibling::*[1]"));
var childRow = childList.FirstOrDefault(x => x.Text.Equals(childSrc)).FindElement(By.XPath("following-sibling::*[1]"));
相同的代码可在我们应用程序的另一个ui上工作.
Same code works on another ui on our application.
我现在已经像下面那样更改了代码,现在我得到了陈旧的元素异常-由于我需要动态地标识此元素,因此我无法使用此处
I have now changed my code like below and now i am getting stale element exception- Since i need to identify this element dynamically i can not use the POM solution mentioned here https://www.softwaretestingmaterial.com/stale-element-reference-exception-selenium-webdriver/#How-To-Overcome-Stale-Element-Reference-Exception-in-Selenium
var childList = Driver.WebDriver.FindElements(By.ClassName("itl-treeNode-title"));
var parent = childList.FirstOrDefault(x => x.Text.Equals(parentSrc)).FindElement(By.XPath("parent::*"));
var parentRow = parent.FindElement(By.ClassName("itl-treenode-content-cover"));
var child = childList.FirstOrDefault(x => x.Text.Equals(childSrc)).FindElement(By.XPath("parent::*"));
var childRow = child.FindElement(By.ClassName("itl-treenode-content-cover"));
childRow.Click();
//try
//{
// (new Actions(Driver.WebDriver)).DragAndDrop(childRow, parent).Perform();
//}
//catch (Exception ex)
//{
// throw new Exception("Failed to perform drag and drop ");
//}
new Actions(Driver.WebDriver).ClickAndHold(childRow)
.MoveToElement(parent)
.Release(parent)
.Build()
.Perform();
Driver.Wait();
例外
OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=77.0.3865.120)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.PerformActions(IList`1 actionSequenceList)
at OpenQA.Selenium.Interactions.Actions.Perform()
推荐答案
在单击childRow之后,再次构建Webelement以避免陈旧元素引用异常,请参见下面的示例代码.
After clicking on childRow, build the webelement again to avoid stale element reference exception, sample code below.
var childList = Driver.WebDriver.FindElements(By.ClassName("itl-treeNode-title"));
var parent = childList.FirstOrDefault(x => x.Text.Equals(parentSrc)).FindElement(By.XPath("parent::*"));
var parentRow = parent.FindElement(By.ClassName("itl-treenode-content-cover"));
var child = childList.FirstOrDefault(x => x.Text.Equals(childSrc)).FindElement(By.XPath("parent::*"));
var childRow = child.FindElement(By.ClassName("itl-treenode-content-cover"));
childRow.Click();
childRow = child.FindElement(By.ClassName("itl-treenode-content-cover"));
new Actions(Driver.WebDriver).ClickAndHold(childRow)
.MoveToElement(parent)
.Release(parent)
.Build()
.Perform();
Driver.Wait();
这篇关于使用Selenium和C#进行draggable = true时,拖放操作无法使用操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!