拖放不适用于Java中的Chrome WebDriver [英] Drag and Drop not working with chrome webdriver in java
问题描述
在此处输入图片描述,我有以下代码将项目从一个位置拖放到另一位置
Enter image description here I have the following code for drag and drop an item from one location to another location
By sourceLocatorDragAndDrop = By.cssSelector("#available_objects_parent tbody tr td:eq(4)");
By destinationLocatorDragAndDrop = By.cssSelector("#assigned_objects_parent table tbody");
Actions action = new Actions(webDriver);
action.dragAndDrop(webDriver.findElement(sourceLocatorDragAndDrop) ,webDriver.findElement(destinationLocatorDragAndDrop)).build().perform();
此代码给出以下错误:
org.openqa.selenium.InvalidSelectorException:无效的选择器:An 指定了无效或非法的选择器(会话信息: chrome = 74.0.3729.131)(驱动程序信息:chromedriver = 2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform = Windows NT 10.0.17134 x86_64)(警告:服务器未提供任何堆栈跟踪信息)
org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified (Session info: chrome=74.0.3729.131) (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
任何人都可以告诉您如何解决此问题吗?
Can anyone tell how to fix this issue?
推荐答案
您还可以使用JavaScript:-
You can use JavaScript also:-
由于HTML5 Action draganddrop
函数不起作用,我使用了javascript,它对我来说很好用:-
Because in HTML5 Action draganddrop
function is not working,I use javascript and its working fine for me:-
WebElement From = driver.findElement(By.id("sourceImage"));
WebElement To = driver.findElement(By.id("targetDiv"));
//HTML 5
final 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);";
((JavascriptExecutor)driver).executeScript(java_script, From, To);
使用Actions
的代码如下:-
WebElement From = driver.findElement(By.id("sourceImage"));
WebElement To = driver.findElement(By.id("targetDiv"));
Actions builder = new Actions(driver);
Action dragAnddrop = builder.clickAndHold(From)
.moveToElement(To)
.release(To)
.build();
dragAnddrop.perform();
使用firefox IDE查找xpath. 有关更多信息,请通过此链接.
Use firefox IDE for finding xpath. For more information go through this link.
这篇关于拖放不适用于Java中的Chrome WebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!