【问题标题】:Select Value on the page according to spreadsheet - Java - Selenium Webdriver根据电子表格选择页面上的值 - Java - Selenium Webdriver
【发布时间】:2016-03-13 14:15:17
【问题描述】:

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

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

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


    }

}

表:

目标网站:

【问题讨论】:

    标签: java excel selenium selenium-webdriver jxl


    【解决方案1】:

    大概您的问题是您需要按文本选择(而不是按索引),并且还要处理您的输入文档具有“volvo”而&lt;select&gt; 具有“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);
    

    【讨论】:

    • 完美。我在页面上放了一个带有单选按钮的 div 也具有相同的概念,查看电子表格并选择一个页面,我也提出了这个疑问,如果你能再次帮助我,我谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多