根据电子表格select页面上的值 – Java – Selenium Webdriver

我需要阅读电子表格的值,并在页面上select相同的值。

使用下面的代码,我可以读取电子表格的第一列和第二列,并填写页面上的字段,但是我无法读取第三列并selectSelect页面的值。

如何使用Java和Selenium?

package testeplanilha; import java.io.File; import java.io.IOException; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.Select; public class testePlanilha { public static void main(String[] args) throws BiffException, IOException { String nome = ""; String sobrenome= ""; String tipo = ""; WebDriver driver = new FirefoxDriver(); driver.get("file:///c:/index.html"); driver.manage().window().maximize(); Workbook workbook = Workbook.getWorkbook(new File("C:/plan.xls")); Sheet sheet = workbook.getSheet(0); int rowCount = sheet.getRows(); for(int i = 0; i < rowCount; i++){ String nome2 = sheet.getCell(0, i).getContents(); String Sobrenome2 = sheet.getCell(1, i).getContents(); String tipo2 = sheet.getCell(2, i).getContents(); driver.findElement(By.id("nome")).sendKeys(nome2); driver.findElement(By.id("sobrenome")).sendKeys(Sobrenome2); } workbook.close(); Select verificaOpt = new Select(driver.findElement(By.id("select"))); System.out.println("Size: " + verificaOpt.getOptions().size()); } } 

片: 片

目标网站:

目标网站

大概你的问题是,你需要通过文本(而不是索引)来select,并处理的事实,你的input文件有“沃尔沃”,而<select>有“沃尔沃”:

 Select verificaOpt = new Select(driver.findElement(By.id("select"))); // as before String titleCaseType = tipo2.substring(0,1).toUpperCase() + tipo2.substring(1); verificaOpt.selectByVisibleText(titleCaseType);