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

我使用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); } 

您可以尝试在每次input后发送一个TAB键事件。

 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