无法使用ArrayList在selenium中的Sendkeys中传递字符串值

时间:2016-09-30 07:30:52

标签: java selenium

由于我是硒的新手,我试图从Excel工作表中读取数据,格式为.xlsx ...问题是我无法在此处传递uName和pWord值。我正在使用Eclipse ide(Galileo),jdk 1.7 ..使用Apache的POI库。

以下是编码,请建议我。非常感谢您的预期。

public class TestDataExcl {

/**
 * @param args
 * @throws Throwable 
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    TestDataExcl tDataObj = new TestDataExcl();
    ArrayList<String> uName = autoInputFromList(0);//Ive tried as ArrayList uName = autoInputFromList(0)..
    ArrayList<String> pWord = autoInputFromList(1);
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.facebook.com/");
    driver.findElement(By.id("email")).sendKeys(uName);//Error as:The method sendKeys(CharSequence...) in the type WebElement is not applicable for the arguments (ArrayList<String>)
    driver.findElement(By.id("pass")).sendKeys(pWord);//Same as the abover error, it is not recognizing uName, pWord..
    //tDataObj.autoInputFromList(0);
}
    //-------------------------------Reading Data From Excel File-----------------------------//
    public static ArrayList<String> autoInputFromList(int colNo) throws Exception{
        String filePath = "D:\\firstFile.xlsx";
        FileInputStream fisObj = new FileInputStream(filePath);
        XSSFWorkbook wbObj = new XSSFWorkbook(fisObj);
        XSSFSheet shObj = wbObj.getSheet("Sheet1");
        //Sheet shObj = wbObj.getSheetAt(0);
        Iterator<Row> rowIt = shObj.iterator();
        //<Row> int i = 0;
        ArrayList<String> list = new ArrayList<String>();
        rowIt.next();
        while(rowIt.hasNext()){
            list.add(rowIt.next().getCell(colNo).getStringCellValue());
        }System.out.println("List::"+list);
        return list;
    }

}

3 个答案:

答案 0 :(得分:1)

sendKeys()接受String类型参数,而不是List。

答案 1 :(得分:1)

因为,你的autoInputFromList()方法返回一个String类型的ArrayList,所以可能得到第一个项目并将其分配给uName&amp; pWord分别如下:

    String uName = autoInputFromList(0).get(0);
    String pWord = autoInputFromList(1).get(0);

答案 2 :(得分:0)

sendKeys方法将Stringarray of String (String[])作为参数。

您可以将值作为字符串传递,也可以转换ArrayList to String[]

driver.findElement(By.id("email")).sendKeys(uName.toArray(new String[0]));
  

如果您使用的是java 8:

driver.findElement(By.id("email")).sendKeys(uName.stream().toArray(String[]::new));