【问题标题】:Unable to perform Drag and Drop- Webdriver Java无法执行拖放 - Webdriver Java
【发布时间】:2014-03-07 06:07:15
【问题描述】:

我尝试在谷歌图片中搜索文本,然后拖放元素。然而,它的表现并不相同。当我们手动移动鼠标时,会发生一些动作,但不会执行拖放功能。

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

【问题讨论】:

    标签: java testing selenium automation selenium-webdriver


    【解决方案1】:

    将此元素拖动到偏移量的代码是

    WebElement draggable = browser.findElement(By.id("draggable"));新的 Actions(browser).dragAndDropBy(draggable, 200, 10).build().perform();

    【讨论】:

      【解决方案2】:

      试试这个...

      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
      

      【讨论】:

      • 我已经尝试了这两种方法,但它仍然以相同的方式工作。当手动移动鼠标时,它会显示一些拖动动作,但不会执行拖放操作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      相关资源
      最近更新 更多