简体   繁体   English

使用带有 Java 的 Selenium WebDriver 在 iF​​rame 中拖放不起作用

[英]Drag and Drop doesn't work inside iFrame with Selenium WebDriver with Java

I am trying to perform drag and drop with Selenium and Java and it is not working.. What can be the cause.. It doesn't give me any error but it is just not happening..我正在尝试使用 Selenium 和 Java 执行拖放操作,但它不起作用.. 可能是什么原因.. 它没有给我任何错误,但它只是没有发生..

Here is my code.这是我的代码。

public class ActionDragDrop {

    public static void main(String[] args) throws InterruptedException {

        WebDriver driver = new ChromeDriver();
        driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        WebElement f=driver.findElement(By.xpath("//*[@id='iframeResult']"));
        driver.switchTo().frame(f);

        WebElement drag = driver.findElement(By.xpath("//*[@id='drag1']"));
        WebElement drop = driver.findElement(By.xpath("//*[@id='div1']"));

        Actions builder = new Actions(driver);
        Actions dragAndDrop = builder.clickAndHold(drag);
        builder.moveToElement(drop);
        builder.release(drop);
        builder.build();
        dragAndDrop.perform();  
    } 
}

You will to switch to iframe first in order to perform drag and drop event:您将首先切换到 iframe 以执行拖放事件:

driver.switchTo().frame(0);                      //Move inside to the frame. 
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement from = driver.findElement(By.xpath("//your xpath")); 
Actions act = new Actions(driver);
act.clickAndHold(from).build().perform();
Thread.sleep(4000);
driver.switchTo().defaultContent();              //Move outside to the frame.

driver.switchTo().frame(1);                     //Move inside to another frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement to = driver.findElement(By.id("guide_RIGHT_SAFETY_rect"));
act.clickAndHold(to).moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
driver.switchTo().defaultContent();                //Move outside to another frame.

Note : Kindly use your xpath, id, classname etc, I have just copied an example.注意:请使用您的 xpath、id、classname 等,我刚刚复制了一个示例。 More or less idea should be same.或多或少的想法应该是相同的。

Try this below code.试试下面的代码。

As your from and to webelement is located inside the same iframe .由于您的fromto webelement 位于同一个iframe First you need to switch inside the iframe.首先你需要在iframe.里面切换iframe.

driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.manage().window().maximize();

driver.switchTo().frame("iframeResult");   //Move inside to the frame. 
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement from = driver.findElement(By.xpath("//img[@id='drag1']")); 
WebElement to = driver.findElement(By.xpath("//div[@id='div1']"));

Actions act = new Actions(driver);
act.clickAndHold(from).perform();
Thread.sleep(4000);
act.clickAndHold().moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
driver.switchTo().defaultContent();    //Move outside to the frame.

I have done many experiments and finally found the solution as below code in Python.我做了很多实验,最后在 Python 中找到了如下代码的解决方案。

The failure at DragAndDrop is not caused by iframe. DragAndDrop 的失败不是由 iframe 引起的。

Just separate every step and perform it.只需将每一步分开并执行即可。

# drag leftbox and drop on rightbox

actions = ActionChains(driver)
actions.click_and_hold(leftbox).perform()
sleep(4)
actions.move_to_element(rightbox).perform()
sleep(4)
actions.release(rightbox).perform()

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

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