如何使用selenium webdriver设置自动完成下拉值

时间:2016-05-16 06:19:31

标签: excel selenium selenium-webdriver

我使用excel表传递值,当我设置下拉值时,它会在自动完成下拉列表中设置数据,但是当它转到下一个下拉列表时,它会删除上一个下拉列表的设置值。这是我的代码。

    public void createreceipt(WebDriver driver,  String clientRef, String agreeBatchReferenceNo, String debtor) throws InterruptedException, IOException {

    driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[1]/ul/li[5]/a")).click();
    driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div[2]/ul/div[2]/li/a")).click();
    Thread.sleep(1000);
    driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[3]/div/div[2]/div/form/div[1]/ul/input[1]")).click();           //Click on add

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    receiptBatchReferenceNo = driver.findElement(By.id("batchReferenceNo")).getAttribute("value");

    setReceiptBatchReferenceNo(receiptBatchReferenceNo);

    Thread.sleep(5000);
    //To select Client
    driver.findElement(By.id("clientName_combobox")).sendKeys(clientRef);

    Thread.sleep(5000); 

    //To select agreement
    driver.findElement(By.id("agreementNo_combobox")).sendKeys(agreeBatchReferenceNo);

    Thread.sleep(3000);

    //To select debtor
    driver.findElement(By.id("debtor_combobox")).sendKeys(debtor);

    Thread.sleep(3000);
    File src= new File("C:\\Users\\orcilia_84\\Desktop\\receipt.xlsx");
    FileInputStream fs = new FileInputStream(src);                      //To get file (to read)
    XSSFWorkbook wb = new XSSFWorkbook(fs);                             //
    XSSFSheet sheet = wb.getSheetAt(0);                                //to get sheet


    //Date
    Date data1 = (Date)sheet.getRow(1).getCell(0).getDateCellValue();
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    String reportDate = df.format(data1).toString();
    driver.findElement(By.id("datepicker")).sendKeys(reportDate);

    // Payment type
    String data2 = sheet.getRow(1).getCell(1).getStringCellValue();
    driver.findElement(By.id("paymentType")).sendKeys(data2);

    //Factor bank acct
    String data3 = sheet.getRow(1).getCell(2).getStringCellValue();
    driver.findElement(By.id("factorsBankAccount")).sendKeys(data3);

    //Recpt curency
    String data4 = sheet.getRow(1).getCell(3).getStringCellValue();
    driver.findElement(By.id("receiptCurrency")).sendKeys(data4);

    // Recpt Amt
    Long data5 = (long) sheet.getRow(1).getCell(4).getNumericCellValue();
    driver.findElement(By.id("receiptAmount")).sendKeys(data5.toString());

    //To submit

      JavascriptExecutor executor = (JavascriptExecutor)driver;
      executor.executeScript("arguments[0].click();", driver.findElement(By.id("ajaxSave")));
      Thread.sleep(2000);

}

1 个答案:

答案 0 :(得分:1)

您可以尝试在每次输入后发送TAB-Key事件。

WebElement element = driver.findElement(By.id("datepicker"));
element.sendKeys(reportDate);
// send the TAB-Key
element.sendKeys("\t");

// do the same for each input dropdown field