mardi 28 juillet 2020

Drag and Drop function is not working in selenium Edge Browser

Following is HTML for source element:

<div title="" class="dragItem row " id="DocSelected-1" draggable="true" type="11" data-content="Salary Slips" data-draggable="item" document-required="1" state="notVerified" accountid="0">
                                    <span class="col-xs-10 dl-no-padding">
                                        <span>Salary Slips</span>
                                    </span>

and following is HTML for target element:

    <div class="recievedDocumentContainer" id="div3" data-draggable="targetRD">
 <p class="col-heading">                    
Received Documents                
</p>                
<div class="recievedDocumentsDivforScroll">                    
<div class="slimScrollDiv" style="width: auto; height: 67px; overflow: hidden; position: relative;">
<div class="recievedDocs" style="width: auto; height: 67px; overflow: hidden;">                    
</div>

Below are the different methods tried to achieve drag and drop function but nothing worked.

All the methods are getting passed in console but drag and drop is not performed.

//method 1 - action class
            IWebElement from = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span"));
            IWebElement to = driver.FindElement(By.XPath("//*[@id='div3']/div[1]/div/div[1]"));
            Actions act = new Actions(driver);
            act.DragAndDrop(from, to);
            act.Build().Perform();
            System.Threading.Thread.Sleep(5000);
        Assert.IsTrue(driver.FindElement(By.XPath("//[@id='DocValidatePopup']/div/div/h3/b")).Displayed);


//method 2 -action class with x/y offset
            IWebElement from = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span"));
            Actions act = new Actions(driver);
            act.DragAndDropToOffset(from, 1300, 234).Build().Perform();

 //method 3
            var ele1 = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span"));
            var ele2 = driver.FindElement(By.XPath("//*[@id='div3']/div[1]/div/div[1]"));
            DragAndDrop(ele1, ele2);
            void DragAndDrop(IWebElement element1, IWebElement element2)
            {
                var builder = new Actions(driver);
                var dragAndDropbuilder.ClickAndHold(element1).MoveToElement(element2).Release(element2).Build();
                dragAndDrop.Perform();
            }

 //method 4
            var ele1 = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span"));
            var ele2 = driver.FindElement(By.XPath("//*[@id='div3']/div[1]/div/div[1]"));
            DragAndDrop(ele1, ele2);
            void DragAndDrop(IWebElement element1, IWebElement element2)
            {
                var builder = new Actions(driver);
                var dragAndDrop = builder.ClickAndHold(element1).MoveToElement(element2, 1300,234).Release(element1).Build();
                dragAndDrop.Perform();

            }

 //method 5
            IWebElement from = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span"));
            IWebElement to = driver.FindElement(By.XPath("//*[@id='div3']/div[1]/div/div[1]"));
            Actions act = new Actions(driver);
            act.ClickAndHold(from).MoveToElement(to).Release().Build();

 //method 6 Javascript
            String java_script = "var src=arguments[0],tgt=arguments[1];var dataTransfer={dropEffe" + "ct:'',effectAllowed:'all',files:[],items:{},types:[],setData:fun" + "ction(format,data){this.items[format]=data;this.types.append(for" + "mat);},getData:function(format){return this.items[format];},clea" + "rData:function(format){}};var emit=function(event,target){var ev" + "t=document.createEvent('Event');evt.initEvent(event,true,false);" + "evt.dataTransfer=dataTransfer;target.dispatchEvent(evt);};emit('" + "dragstart',src);emit('dragenter',tgt);emit('dragover',tgt);emit(" + "'drop',tgt);emit('dragend',src);";
            IWebElement from = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span"));
            IWebElement to = driver.FindElement(By.XPath("//*[@id='div3']/div[1]/div/div[1]"));
            IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
            jse.ExecuteScript(java_script, from, to);

  //method 7 Javascript
            IWebElement from = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span"));
            IWebElement to = driver.FindElement(By.XPath("//*[@id='div3']/div[1]/div/div[1]"));
            IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
            jse.ExecuteScript("function createEvent(typeOfEvent) {\n" + "var event =document.createEvent(\"CustomEvent\");\n" + "event.initCustomEvent(typeOfEvent,true, true, null);\n" + "event.dataTransfer = {\n" + "data: {},\n" + "setData: function (key, value) {\n" + "this.data[key] = value;\n" + "},\n" + "getData: function (key) {\n" + "return this.data[key];\n" + "}\n" + "};\n" + "return event;\n" + "}\n" + "\n" + "function dispatchEvent(element, event,transferData) {\n" + "if (transferData !== undefined) {\n" + "event.dataTransfer = transferData;\n" + "}\n" + "if (element.dispatchEvent) {\n" + "element.dispatchEvent(event);\n" + "} else if (element.fireEvent) {\n" + "element.fireEvent(\"on\" + event.type, event);\n" + "}\n" + "}\n" + "\n" + "function simulateHTML5DragAndDrop(element, destination) {\n" + "var dragStartEvent =createEvent('dragstart');\n" + "dispatchEvent(element, dragStartEvent);\n" + "var dropEvent = createEvent('drop');\n" + "dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n" + "var dragEndEvent = createEvent('dragend');\n" + "dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n" + "}\n" + "\n" + "var source = arguments[0];\n" + "var destination = arguments[1];\n" + "simulateHTML5DragAndDrop(source,destination);", from, to);

  //method 8
            IWebElement from = driver.FindElement(By.XPath("//*[@id='DocSelected-1']/span"));
            IWebElement to = driver.FindElement(By.XPath("//*[@id='div3']/div[1]/div/div[1]"));
            var builder = new Actions(driver);
            builder.ClickAndHold(from);
            builder.MoveToElement(to, 1300, 234);
            builder.Perform();
            System.Threading.Thread.Sleep(2000);
            builder.Release(to); builder.Perform();

Kindly suggest any other alternative methods to perform drag and drop.

Used:

  • Language - C#
  • Selenium Webdriver
  • Edge Browser

Aucun commentaire:

Enregistrer un commentaire