Select 根据电子表格在页面上的值 - Java - Selenium Webdriver
Select Value on the page according to spreadsheet - Java - Selenium Webdriver
我需要读取电子表格值和 select 页面上的相同值。
使用下面的代码,我可以读取电子表格的第一列和第二列并填写页面上的字段,但无法读取第三列和 select Select 页 .
如何使用 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());
}
}
Sheet:
目标站点:
大概你的问题是你需要 select 按文本(而不是按索引),并且还要处理这样一个事实,即你的输入文档有 "volvo" 而 <select>
有"Volvo":
Select verificaOpt = new Select(driver.findElement(By.id("select"))); // as before
String titleCaseType = tipo2.substring(0,1).toUpperCase() + tipo2.substring(1);
verificaOpt.selectByVisibleText(titleCaseType);
我需要读取电子表格值和 select 页面上的相同值。
使用下面的代码,我可以读取电子表格的第一列和第二列并填写页面上的字段,但无法读取第三列和 select Select 页 .
如何使用 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());
}
}
Sheet:
目标站点:
大概你的问题是你需要 select 按文本(而不是按索引),并且还要处理这样一个事实,即你的输入文档有 "volvo" 而 <select>
有"Volvo":
Select verificaOpt = new Select(driver.findElement(By.id("select"))); // as before
String titleCaseType = tipo2.substring(0,1).toUpperCase() + tipo2.substring(1);
verificaOpt.selectByVisibleText(titleCaseType);