2017-11-08 2 views
0

フレームの上にドラッグされたすべての要素が反復されるように、ドロップされます。クラス名を繰り返して、同じクラス名の関連オブジェクトをすべてjavaのセレニウムでクリックするようにします

これを達成するには、以下のコードを書いたところで、getClassがすべての要素を取得する必要があるようです。 getClassが削除されると、2つの要素だけが検出され、さらに多くの要素が削除されるということです。

「getClass」を持っている必要があり、すべての要素がクリックされるようにa.ui-icon.ui-icon-refreshのクラス全体を取得する別の方法がありますか?すべての

package Testing; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 
import java.util.Iterator; 
import java.util.List; 


public class Radiobuttons { 


     public static void main(String[] args) 
    { 
      // TODO Auto-generated method stub 
      System.setProperty("webdriver.chrome.driver","C://chromedriver.exe"); 
      WebDriver driver=new ChromeDriver(); 
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
      driver.get("https://jqueryui.com/droppable/"); 
      System.out.println(driver.getTitle()); 
      WebElement SimplePhotoManager = driver.findElement(By.xpath("//*[@id=\"content\"]/div[1]/ul/li[5]/a")); 
      SimplePhotoManager.click(); 
      driver.switchTo().frame(driver.findElement(By.xpath("//*[@id=\"content\"]/iframe"))); 
      WebElement source1 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[1]")); 
      WebElement source2 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[2]")); 
      WebElement source3 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[3]")); 
      WebElement source4 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[4]")); 
      WebElement targetBefore = driver.findElement(By.xpath("//*[@id=\"trash\"]")); 
      Actions a = new Actions(driver); 
      a.dragAndDrop(source1, targetBefore).build().perform(); 
      a.dragAndDrop(source2, targetBefore).build().perform(); 
      a.dragAndDrop(source3, targetBefore).build().perform(); 
      a.dragAndDrop(source4, targetBefore).build().perform(); 
      //Why is this required? 
      List<WebElement> getClass= driver.findElements(By.className("a.ui-icon")); 
      //Grab common attribute//Put into list and iterate 
      int count=driver.findElements(By.className("ui-icon-refresh")).size(); 
      System.out.println(count); 
      for(int i=0;i<count;i++) 
      { 
       driver.findElements(By.className("ui-icon-refresh")).get(i).click(); 
      } 

      //Close the thing 
      //driver.quit(); 
    } 


} 

答えて

0

まず、あなたはここに明示的な待機を使用する必要があります。

WebElement SimplePhotoManager = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='content']/div[1]/ul/li[5]/a"))); 
     SimplePhotoManager.click(); 

時々私は要素がクリックできないという例外がありますので。あなたが呼び出す問題について

、:

List<WebElement> getClass= driver.findElements(By.className("a.ui-icon")); 

実際、後に、このリストに何もしません。

int count=driver.findElements(By.className("ui-icon-refresh")).size(); 

完全なリストを「見る」こと:

理由この呼び出しは少し時間が「失われた」と、これは、次の呼び出しでできるためである「それが動作」の後にあるため。

これを確認するには、ドロップされた要素を明示的に待機させることができます。たとえば、拳の場合は、

WebElement first = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"trash\"]/ul/li[1]"))); 

を入力してください。

最後に、あなたのループ内には、必ず呼び出す:

driver.findElements(By.className("ui-icon-refresh")) 

はそれを行う必要はありません。

内部ですべての要素を一度、繰り返して呼び出すことができます。

全体コード:

 System.setProperty("webdriver.chrome.driver","C://chromedriver.exe"); 
     WebDriver driver=new ChromeDriver(); 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
     driver.get("https://jqueryui.com/droppable/"); 
     System.out.println(driver.getTitle()); 
     WebElement SimplePhotoManager = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='content']/div[1]/ul/li[5]/a"))); 
     SimplePhotoManager.click(); 
     driver.switchTo().frame(driver.findElement(By.xpath("//*[@id=\"content\"]/iframe"))); 
     WebElement source1 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[1]")); 
     WebElement source2 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[2]")); 
     WebElement source3 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[3]")); 
     WebElement source4 = driver.findElement(By.xpath("//*[@id=\"gallery\"]/li[4]")); 
     WebElement targetBefore = driver.findElement(By.xpath("//*[@id=\"trash\"]")); 
     Actions a = new Actions(driver); 
     a.dragAndDrop(source1, targetBefore).build().perform(); 
     WebElement first = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"trash\"]/ul/li[1]"))); 
     a.dragAndDrop(source2, targetBefore).build().perform(); 
     WebElement second = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"trash\"]/ul/li[2]"))); 
     a.dragAndDrop(source3, targetBefore).build().perform(); 

     WebElement third = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"trash\"]/ul/li[3]"))); 
     a.dragAndDrop(source4, targetBefore).build().perform(); 
     //Why is this required? 
     WebElement fourth = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"trash\"]/ul/li[4]"))); 


     System.out.println("-----------------"); 

     //List<WebElement> getClass= driver.findElements(By.className("a.ui-icon")); 
     //Grab common attribute//Put into list and iterate 

     List<WebElement> wel= driver.findElements(By.className("ui-icon-refresh")); 
     int count= wel.size(); 
     System.out.println("-----------------"); 
     System.out.println(count); 

     for(WebElement we: wel) 
     { 
      we.click(); 
     } 
関連する問題