简体   繁体   English

无法执行拖放-Webdriver Java

[英]Unable to perform Drag and Drop- Webdriver Java

I have tried to search a text in google images and then drag the element and drop. 我试图在Google图片中搜索文本,然后将其拖放。 However it is not performing the same. 但是,它执行的效果不一样。 When we manually move the mouse some action is occurring but drag and drop function is not performed. 当我们手动移动鼠标时,会发生一些动作,但是没有执行拖放功能。

WebDriver driver= new FirefoxDriver();
    driver.get("http://images.google.com/");
    Thread.sleep(1000);
    driver.findElement(By.name("q")).sendKeys("Apple");
    driver.findElement(By.name("btnG")).click();
    WebElement drag= driver.findElement(By.cssSelector("html body#gsr.srp div#main div#cnt.mdm div#rcnt div.col div#center_col div#res.med div#topstuff div#ifbc.prc div#ifb.prs a.rg_fbl div.rg_bb div.rg_bb_i div.rg_bb_layout div.rg_di img.rg_i"));
    WebElement drop= driver.findElement(By.cssSelector("html body#gsr.srp div#tphdr.tphdr div#mngb div#gb.gb_1b div.gb_Ab div.gb_k div#gbq div#gbq2.gbt div#gbqfw.gbqfr form#gbqf.gb_Nb fieldset#gbqff.gbqff div#gbfwa.gbqfwa div#gbqfqw.gbqfqw div#gbqfqwb.gbqfqwc table#gs_id0.gstl_0 tbody tr td#gs_tti0.gsib_a div#gs_lc0 input#gs_htif0.gbqfif"));
    Actions builder= new Actions(driver);
    Action dragelement= builder.clickAndHold(drag).build();
    dragelement.perform();
    Action dropelement=builder.moveToElement(drop).release(drop).build();
    dropelement.perform();

Try This... 尝试这个...

WebDriver driver = new FirefoxDriver();    
driver.get("http://images.google.com/");  

driver.switchTo().frame(0);   

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);    

WebElement dragElement = driver.findElement(By.id("draggable"));  

WebElement dropElement = driver.findElement(By.id("droppable")); 

Actions builder = new Actions(driver);  // Configure the Action  

Action dragAndDrop = builder.clickAndHold(dragElement)
                            .moveToElement(dropElement)
                            .release(dropElement).build();  // Get the action  

dragAndDrop.perform(); // Execute the Action

code to drag this element to an offset is 将该元素拖动到偏移量的代码是

WebElement draggable = browser.findElement(By.id("draggable")); WebElement draggable = browser.findElement(By.id(“ draggable”)); new Actions(browser).dragAndDropBy(draggable, 200, 10).build().perform(); new Actions(浏览器).dragAndDropBy(可拖动,200,10).build()。perform();

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

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