拖放在selenium代码中不起作用
拖放在我下面的selenium代码中不起作用,有人能帮我吗拖放在selenium代码中不起作用,selenium,Selenium,拖放在我下面的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.FirefoxDrive
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();
}
}
您的拖放元素显示在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 你需要先换一个
WebElement from= driver.findElementByXPath("html/body/div[1]");
与
及
与
您可以使用以下代码来计算:
driver.navigate().to("http://jqueryui.com/droppable/");
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
dragAndDrop(Sourcelocator,Destinationlocator);
通过一些简单的调整找到您自己的工作代码块:
//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();
您当然可以使用下面的代码来完成它。只需确保您的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();
如果有任何问题,请告诉我,但试试这一个,它会起作用的:如果您能进一步解释您的问题,以便其他查看者能够理解您的问题,那就更好了。
driver.navigate().to("http://jqueryui.com/droppable/");
//Wait for the frame to be available and switch to it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame")));
WebElement Sourcelocator = driver.findElement(By.cssSelector(".ui-draggable"));
WebElement Destinationlocator = driver.findElement(By.cssSelector(".ui-droppable"));
dragAndDrop(Sourcelocator,Destinationlocator);
//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();
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();