拖放不能在selenium代码中工作

时间:2017-05-24 08:38:03

标签: selenium

拖放功能在我的硒代码中无效,有人可以帮助我吗?

package selenium;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.BasicConfigurator;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.WebElement;


public class draganddrop {

    public static void main(String[] args){

        BasicConfigurator.configure();
        FirefoxDriver driver=new FirefoxDriver();
        driver.get("http://jqueryui.com/droppable/");
        driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        WebElement  from= driver.findElementByXPath("html/body/div[1]");
        WebElement  to=driver.findElementByXPath("html/body/div[2]");
        new Actions(driver).dragAndDrop(from, to).build().perform();


        //Actions mouseoveron=new Actions(driver);
        //mouseoveron.click().dragAndDrop(from, to).build().perform();


    }

}

4 个答案:

答案 0 :(得分:0)

您的拖放元素位于iFrame标记下。首先,您需要切换到框架然后执行拖放操作。

使用以下代码:

System.setProperty("webdriver.chrome.driver","D:/Application/chromedriver.exe");
driver = new ChromeDriver();        
driver.manage().window().maximize();
driver.get("http://jqueryui.com/droppable/");               
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
driver.switchTo().frame(0); // either use index or frame element
WebElement  from= driver.findElement(By.id("draggable"));
WebElement  to=driver.findElement(By.id("droppable"));
new Actions(driver).dragAndDrop(from, to).build().perform();

请注意:如果元素ID在那里,请使用id选择器而不是xpath

答案 1 :(得分:0)

我在您的代码中发现的几件事

您需要先替换

var data = {    
  count: 11,    
  length: 27,    
  days: 3    
};    

var fromServer = [{field: 'count', operator: '>', value: '10'}, {field: 'length', operator: '>', value: '3'}];    

if (checkObjConditions(fromServer)) {    
  console.log("yes");    
} else {    
  console.log("no");    
}    

function checkObjConditions(co) {    
  //var conditions = c.split("&&");    
  var isCondition = true;    
  for (var a = 0; isCondition && a < co.length; a++) {    
    //var c = conditions[a].trim().split(",");    
    var r = compare(co[a]['field'], co[a]['operator'], co[a]['value']);    
    console.log(">", r);    
    if (!r)    
      isCondition = false;    
  }    
  return isCondition;    
}    

function compare(a, operator, b) {    
  var ans = false;    
  switch (operator) {    
    case '<':    
      if (data[a] < parseInt(b))    
        ans = true;    
      break;    
    case '>':    
    console.log(data[a], parseInt(b))    
      if (data[a] > parseInt(b))    
        ans = true;    
      break;
    // ... and other cases also    
  }    
  return ans;    
}

WebElement  from= driver.findElementByXPath("html/body/div[1]");

并且

 WebElement from = driver.findElement(By.xpath("html/body/div[1]"))

WebElement  to=driver.findElementByXPath("html/body/div[2]");

您可以使用以下代码来解决问题:

WebElement  to = driver.findElement(By.xpath("html/body/div[2]"));

答案 2 :(得分:0)

通过一些简单的调整找到你自己的工作代码块:

//BasicConfigurator.configure();
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
WebElement  from = driver.findElement(By.id("draggable"));
WebElement  to = driver.findElement(By.id("droppable"));
new Actions(driver).dragAndDrop(from, to).build().perform();

答案 3 :(得分:0)

您可以使用以下代码完成此操作。只需确保您的xpath正确无误,否则您无法使用任何选项执行此操作。

WebElement from = driver.findElement(By.xpath("html/body/div[1]"));
WebElement  to = driver.findElement(By.xpath("html/body/div[2]"));
Actions action1 = new Actions(driver);
action1.clickAndHold(from).moveToElement(to).release(from).build().perform();

如果有任何问题,请告诉我,但尝试一下它会起作用:)