根据电子表格选择页面上的值 - Java - Selenium Webdriver

时间:2016-03-13 14:15:18

标签: java excel selenium selenium-webdriver jxl

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

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

如何使用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

目标网站:

target site

1 个答案:

答案 0 :(得分:1)

大概你的问题是你需要通过文本(而不是索引)来选择,并且还要处理你的输入文档有&#34; volvo&#34;而Select verificaOpt = new Select(driver.findElement(By.id("select"))); // as before String titleCaseType = tipo2.substring(0,1).toUpperCase() + tipo2.substring(1); verificaOpt.selectByVisibleText(titleCaseType); 有&#34;沃尔沃&#34;:

{{1}}