2016-03-09 94 views
0

我有一個數據列表的數組列表,硒必須用這個列表列表中的數據填寫一個字段,但是它們的排列順序與列表中的列表相同。 在他放棄元素的那一刻,我需要他拿回元素1,然後他回來跑去拿起元素2,然後拿起元素3,等等。Arraylist Sequential em Java - Selenium WebDriver

我該如何得到他的元素。

package promocoes; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.ie.InternetExplorerDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class criarPromocoes { 

    public static void main(String[] args) { 


     System.setProperty("webdriver.ie.driver", "C:/Users/paulo.roberto/Documents/eclipse/Selenium/IEDriverServer.exe"); 
      DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); 
      caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 
      WebDriver driver = new InternetExplorerDriver(caps); 

      driver.get("url"); 
      driver.manage().window().maximize(); 

      //wait page 
      WebDriverWait paginainicial = new WebDriverWait(driver, 10); 
      paginainicial.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/center/table/tbody/tr[2]/td/form/table/tbody/tr[5]/td/table/tbody/tr/td/table/tbody/tr[1]/td[2]/input"))); 

      //login 
      driver.findElement(By.xpath("/html/body/center/table/tbody/tr[2]/td/form/table/tbody/tr[5]/td/table/tbody/tr/td/table/tbody/tr[1]/td[2]/input")).sendKeys("login"); 
      driver.findElement(By.xpath("/html/body/center/table/tbody/tr[2]/td/form/table/tbody/tr[5]/td/table/tbody/tr/td/table/tbody/tr[2]/td[2]/input")).sendKeys("password"); 
      driver.findElement(By.xpath("/html/body/center/table/tbody/tr[2]/td/form/input[1]")).click(); 

      //wait page 
      WebDriverWait presencanomePlano = new WebDriverWait(driver, 10); 
      presencanomePlano.until(ExpectedConditions.visibilityOfElementLocated(By.name("nomePromocao"))); 

      //form 

      driver.findElement(By.name("nomePromocao")).sendKeys(nomePromocao());        driver.findElement(By.name("dataInicioPromocao")).sendKeys("24/09/2015");    driver.findElement(By.name("dataFimPromocao")).sendKeys("04/12/2049"); 



    } 


    public static String nomePromocao(){ 
      List<String> lista = new ArrayList<String>(); 


      lista.add ("promo1");    
      lista.add ("promo2"); 
      lista.add ("promo3"); 

      Collections.shuffle (lista); 

      return lista.get(0); 
     } 


} 
+1

列表(ArrayList的)商店的元素通過其indexes..So您可以使用遍歷元素某種指數。另外你需要刪除Collections.shuffle(),因爲你可能會鬆動職位 –

回答

1

如果你的目標是多次填充形式從列表中隨機值:

public static void main(String[] args) { 

    ... 

    //form, iterate over each promotion 
    List<String> promocaoNames = getPromocaoNames(); 
    for(String promocaoName : promocaoNames){ 
    driver.findElement(By.name("nomePromocao")).sendKeys(promocaoName); 
    driver.findElement(By.name("dataInicioPromocao")).sendKeys("24/09/2015"); 
    driver.findElement(By.name("dataFimPromocao")).sendKeys("04/12/2049"); 

    ... 
    } 

    ... 
} 


public static List<String> getPromocaoNames(){ 
    List<String> lista = new ArrayList<String>(); 
    lista.add("promo1");    
    lista.add("promo2"); 
    lista.add("promo3"); 

    Collections.shuffle (lista); 
    return lista; 
}