简体   繁体   English

拖放无法在 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();

This code gives the following error:此代码给出以下错误:

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) org.openqa.selenium.InvalidSelectorException:无效选择器:指定了一个无效或非法的选择器(会话信息:chrome=74.0.3729.131)(驱动程序信息:chromedriver=2.46.628402(536cd7adbad73a3783fdc2cab92ab2ba7ec364eWindows.161630) (警告:服务器没有提供任何堆栈跟踪信息)

Can anyone tell how to fix this issue?谁能告诉如何解决这个问题?

You can use JavaScript also:-您也可以使用 JavaScript:-

Because in HTML5 Action draganddrop function is not working,I use javascript and its working fine for me:-因为在 HTML5 Action draganddrop功能不起作用,我使用 javascript 并且它对我来说工作正常:-

    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);

And using Actions the code is below:-并使用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();

Use firefox IDE for finding xpath.使用 firefox IDE 查找 xpath。 For more information go through this link.有关更多信息,请访问链接。

It seems you are using wrong cssSelector.看来您使用了错误的 cssSelector。 You always can validate xpath in chrome developer options.您始终可以在 chrome 开发人员选项中验证 xpath。 Please go through below link.请通过以下链接。 Please provide html code for the sourceLocatorDragAndDrop and destinationLocatorDragAndDrop so that we can understand what went wrong.请提供 sourceLocatorDragAndDrop 和 destinationLocatorDragAndDrop 的 html 代码,以便我们了解发生了什么问题。

https://yizeng.me/2014/03/23/evaluate-and-validate-xpath-css-selectors-in-chrome-developer-tools/ https://yizeng.me/2014/03/23/evaluate-and-validate-xpath-css-selectors-in-chrome-developer-tools/

:eq() is a JQuery selector, not cssselector . :eq()是一个 JQuery 选择器,而不是cssselector Selenium doesn't recognize it.硒不识别它。 The closest match is :nth-child()最接近的匹配是:nth-child()

By sourceLocatorDragAndDrop = By.cssSelector("#available_objects_parent tbody tr td:nth-child(4)");

I also tried different options.我也尝试了不同的选择。 Eventually this one worked.最终这个成功了。 Actions actions = new Actions(driver);动作动作=新动作(驱动程序); actions.clickAndHold().moveByOffset(0, 100).moveToElement(, 0, 100).release().build().perform(); actions.clickAndHold().moveByOffset(0, 100).moveToElement(, 0, 100).release().build().perform(); Thread.sleep(3000);线程睡眠(3000);

Added sleep to wait for element to be dragged before starting further actions.添加了 sleep 以在开始进一步操作之前等待元素被拖动。

For C#, I used this code based on the help of @Pradnya Bolli above, and it works well:对于 C#,我在上面@Pradnya Bolli的帮助下使用了这段代码,它运行良好:

Actions act = new Actions(Driver);
Actions myDragAndDrop = (Actions)act.ClickAndHold(From).MoveToElement(To).Release(To).Build();
myDragAndDrop.Perform();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM