Paulo Roberto
Paulo Roberto

Reputation: 1548

Select Value on the page according to spreadsheet - Java - Selenium Webdriver

I need to read a spreadsheet value and select the same value on the page .

With the code below , I can read the first and second column of the spreadsheet and fill in the fields on the page , but I can not read the third column and select the value of the Select page .

How do this using Java and 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: Sheet

target site:

target site

Upvotes: 0

Views: 619

Answers (1)

Andrew Regan
Andrew Regan

Reputation: 5113

Presumably your problem is that you need to select by text (not by index), and also deal with the fact that your input document has "volvo" whereas the <select> has "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);

Upvotes: 1

Related Questions