2017-05-24 59 views
-1

拖放不工作在我的硒代碼下面,任何人都可以幫助我嗎?拖放不工作在硒代碼

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


    } 

} 
+0

如果任何回答對你有幫助那麼請不要忘記去接受它。請閱讀https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – NarendraR

回答

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的

+0

非常感謝,我的代碼正在工作:) – wilfred

0

幾件事我在你的代碼觀察

您需要先更換

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]")); 

您可以使用下面的代碼摸出:

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

讓我知道如果這個回答你的問題。

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

讓我知道,在任何情況下的問題,但嘗試這一個,將工作:)