问题描述
我正在尝试使用Java中的Selenium Web Driver自动执行IE11中的拖放功能.我能以某种方式在Chrome上实现它,但在IE中却没有.
I am trying to automate drag and drop functionality in IE11 using Selenium Web Driver in Java. I am somehow able to achieve it on Chrome, but it's not happening in IE.
在进一步说明之前,这是我如何拖放:
Before further explanation here is how I'm dragging and dropping:
Actions builder = new Actions(driver);
builder.clickAndHold(sourceElement)
.moveToElement(targetElement)
.release(targetElement)
.build().perform();
在IE中:无需拖放即可选择从源到目标元素的所有文本.我认为这可能是因为它拾取了错误的元素,并尝试了一些相关的父元素和子元素的操作,但是没有用.
In IE: Instead of dragging and dropping it selects all the text from source to destination element. I thought this might be because it's pickup up the wrong element and tried the operation with some relevant parent and child elements but didn't work.
在Chrome浏览器中:该死的工作顺利.
In Chrome: Works damn smooth.
在Firefox中:只需单击保持并单击并拖动,即可执行element no longer attached to DOM exception
.这可能是因为,我要从网格(剑道网格)中拖出一行,并且由于不可能从网格中拖出一行,因此我们的开发人员已经实现了这种方式,当您拖动行时,会创建一个新的动态元素,前进.
In Firefox: Just performs click on holds and while dragging throws, element no longer attached to DOM exception
. This might be because, I am dragging a row from a grid (kendo grid) and since dragging a row from a grid is not possible our devs have implemented it in such a way that when you drag a row a new dynamic element is created which moves along.
只需添加更多详细信息:
Just to add on more details:
- 我已经尝试过
dragAndDrop()
和其他Javacript选项. - 我正在使用最新版本的Selenium和更新的IE.
- 我们的网格使用HTML5组件,我发现那里已经有几个问题了(虽然不确定所有问题是什么),但是由于我的情况是在一种浏览器中工作的,所以我希望这不是这些问题之一.
- 我通过某种方式使使用
Robot
类成为可能,但是它太不可靠并且表现得很怪异,我宁愿放弃而不是使用它.
- I have already tried
dragAndDrop()
and other Javacript options. - I'm using the latest version of selenium and updated IE.
- Our grid uses HTML5 components and I've discovered that there are few issues already there (not sure about what all issues though), but still since my scenario was working in one browser I hope this is not one of those issues.
- I have made it possible somehow using
Robot
class but it is too unreliable and behaves weird, I would prefer giving up than using it.
任何帮助将不胜感激!
推荐答案
如果是HTML5拖放,一种解决方案是使用某些javascript模拟它.这是一个将项目放到垃圾箱中的工作示例:
One solution if it's an HTML5 drag and drop is to simulate it with some javascript.Here is a working example that drops an item to a bin:
final String JS_DnD =
"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);";
WebDriver driver = new InternetExplorerDriver();
driver.get("http://html5demos.com/drag");
WebElement ele_source = driver.findElement(By.id("two"));
WebElement ele_target = driver.findElement(By.id("bin"));
// drag and drop item two into the bin
((JavascriptExecutor)driver).executeScript(JS_DnD, ele_source, ele_target);
这篇关于无法自动化IE11的拖放:Selenium WebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!