【问题标题】:Java -= Selenium Loop with changing variables and Excel importJava -= Selenium Loop,具有可变变量和 Excel 导入
【发布时间】:2017-08-24 20:14:10
【问题描述】:

因此,简而言之,我正在尝试从电子表格 (Excel) 中读取值,然后将这些值分配给变量,然后将它们发布到 Web 表单中。我已经成功地手动完成了这项工作,但我确信有一种更有效的方法可以做到这一点。一开始我不知道电子表格中有多少行,所以我需要一种方法让它循环,直到找到一个空行。

Web 表单的地址都是可预测的,第一个数字代表行(1-PF_7、2-PF_7 等)。

我的代码如下,我提前为它的长度道歉(这就是我在这里寻找指导的原因:)

package automationframework;


import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.DataFormatter;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class RV_Security {


    public static void Execute(WebDriver driver) throws Exception{
            }

//  public static void main(String[] args) {
        // TODO Auto-generated method stub
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {

        {
        String exePath = "C:\\windows\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", exePath);
        WebDriver driver = new ChromeDriver();

//Open website
        driver.manage().window().maximize();
        driver.get("http://atc.la.gov/rvportal.cfm");
        driver.findElement(By.id("securityportalnewnav")).click();
        driver.switchTo().frame(driver.findElement(By.id("MTAzODA2")));
        driver.findElement(By.id("PF_9")).sendKeys("d*****.com");
        driver.findElement(By.id("PF_10")).sendKeys("****");
        driver.findElement(By.id("PF_8")).click();
        driver.switchTo().defaultContent();

//New page - Select R/V Roster Submission page
        String oldTab = driver.getWindowHandle();
        driver.switchTo().frame("MTAzODA2");
        driver.findElement(By.id("PF_49")).click();
///R/V Security Submissions Form
         ArrayList<String> newTab = new ArrayList<String>
(driver.getWindowHandles());
        newTab.remove(oldTab);
            // change focus to new tab
//              driver.switchTo().window(newTab.get(1));

            Set<String> windowId = driver.getWindowHandles();    // get  
window id of current window
            Iterator<String> itererator = windowId.iterator();   

            String mainWinID = itererator.next();
            String  newAdwinID = itererator.next();

            driver.switchTo().window(newAdwinID);


driver.findElement(By.xpath("/html/body/div[6]/div[3]/div/button")).click();
        driver.findElement(By.id("PF_3")).sendKeys("ABSEC LLC");
        driver.findElement(By.id("PF_4")).clear();
        driver.findElement(By.id("PF_4")).sendKeys("8685-P");
        driver.findElement(By.id("PF_69")).sendKeys("David A. Short");
        driver.findElement(By.id("PF_71")).sendKeys("38713-T");     

        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        java.util.Date date = new java.util.Date();
        String datetime = dateFormat.format(date);

        driver.findElement(By.id("PF_32")).sendKeys(datetime);
        driver.findElement(By.id("PF_35")).sendKeys("10");
        driver.findElement(By.id("PF_78")).sendKeys("david@absecllc.com");
        driver.findElement(By.id("PF_37")).sendKeys("ONLINE");

        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
    /** driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        driver.findElement(By.xpath("//*
[@id=\"PF_6pag_left\"]/table/tbody/tr/td[1]/div/span")).click();
        */


        // Get Excel Data

        DataFormatter formatter = new DataFormatter(); //Create formatter

        FileInputStream fis=new 
FileInputStream("D://OneDrive//ABSEC//ATC_Reporting//expdata.xlsx");
        Workbook wb=WorkbookFactory.create(fis);
        Sheet sh=wb.getSheetAt(0);
        Row row=sh.getRow(0);
        Cell yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u1s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u1s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u1s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u1s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u1s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u1s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u1s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u1s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u1s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u1s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u1s11=formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u1s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u1s13 =formatter.formatCellValue(yourCell); 

        row=sh.getRow(1);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u2s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u2s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u2s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u2s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u2s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u2s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u2s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u2s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u2s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u2s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u2s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u2s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u2s13 =formatter.formatCellValue(yourCell); 


        row=sh.getRow(2);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u3s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u3s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u3s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u3s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u3s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u3s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u3s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u3s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u3s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u3s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u3s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u3s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u3s13 =formatter.formatCellValue(yourCell); 


        row=sh.getRow(3);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u4s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u4s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u4s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u4s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u4s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u4s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u4s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u4s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u4s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u4s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u4s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u4s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u4s13 =formatter.formatCellValue(yourCell); 


        row=sh.getRow(4);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u5s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u5s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u5s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u5s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u5s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u5s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u5s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u5s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u5s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u5s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u5s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u5s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u5s13 =formatter.formatCellValue(yourCell); 


        row=sh.getRow(5);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u6s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u6s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u6s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u6s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u6s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u6s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u6s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u6s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u6s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u6s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u6s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u6s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u6s13 =formatter.formatCellValue(yourCell); 


        row=sh.getRow(6);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u7s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u7s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u7s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u7s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u7s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u7s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u7s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u7s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u7s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u7s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u7s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u7s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u7s13 =formatter.formatCellValue(yourCell); 


        row=sh.getRow(7);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u8s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u8s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u8s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u8s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u8s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u8s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u8s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u8s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u8s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u8s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u8s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u8s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u8s13 =formatter.formatCellValue(yourCell); 

        row=sh.getRow(8);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u9s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u9s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u9s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u9s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u9s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u9s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u9s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u9s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u9s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u9s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u9s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u9s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u9s13 =formatter.formatCellValue(yourCell); 

        row=sh.getRow(9);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u10s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u10s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u10s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u10s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u10s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u10s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u10s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u10s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u10s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u10s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u10s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u10s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u10s13 =formatter.formatCellValue(yourCell); 


        row=sh.getRow(10);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u11s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u11s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u11s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u11s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u11s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u11s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u11s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u11s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u11s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u11s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u11s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u11s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u11s13 =formatter.formatCellValue(yourCell); 

        row=sh.getRow(11);
        yourCell=row.getCell(0);
        yourCell.setCellType(Cell.CELL_TYPE_STRING);
        String u12s1 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(1);
        String u12s2 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(2);
        String u12s3 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(3);
        String u12s4 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(4);
        String u12s5 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(5);
        String u12s6 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(6);
        String u12s7 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(7);
        String u12s8 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(8);
        String u12s9 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(9);
        String u12s10 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(10);
        String u12s11 =formatter.formatCellValue(yourCell);
        yourCell=row.getCell(11);
        String u12s12 =formatter.formatCellValue(yourCell); 
        yourCell=row.getCell(12);
        String u12s13 =formatter.formatCellValue(yourCell); 




 //Student 1
        driver.findElement(By.id("1-PF_7")).sendKeys(u1s1);
        driver.findElement(By.id("1-PF_8")).sendKeys(u1s2);
        driver.findElement(By.id("1-PF_9")).sendKeys(u1s3);
        driver.findElement(By.id("1-PF_10")).sendKeys(u1s4);
        driver.findElement(By.id("1-PF_11")).sendKeys(u1s5);
        driver.findElement(By.id("1-PF_12")).sendKeys(u1s6);
        driver.findElement(By.id("1-PF_13")).sendKeys(u1s7);
        driver.findElement(By.id("1-PF_14")).sendKeys(u1s8);
        driver.findElement(By.id("1-PF_15")).sendKeys(u1s9);
        driver.findElement(By.id("1-PF_16")).sendKeys(u1s10);
        driver.findElement(By.id("1-PF_17")).sendKeys(u1s11);
        driver.findElement(By.id("1-PF_18")).sendKeys(u1s12);
        driver.findElement(By.id("1-PF_21")).sendKeys(u1s13);

        //Student 2         
        driver.findElement(By.id("2-PF_7")).sendKeys(u2s1);
        driver.findElement(By.id("2-PF_8")).sendKeys(u2s2);
        driver.findElement(By.id("2-PF_9")).sendKeys(u2s3);
        driver.findElement(By.id("2-PF_10")).sendKeys(u2s4);
        driver.findElement(By.id("2-PF_11")).sendKeys(u2s5);
        driver.findElement(By.id("2-PF_12")).sendKeys(u2s6);
        driver.findElement(By.id("2-PF_13")).sendKeys(u2s7);
        driver.findElement(By.id("2-PF_14")).sendKeys(u2s8);
        driver.findElement(By.id("2-PF_15")).sendKeys(u2s9);
        driver.findElement(By.id("2-PF_16")).sendKeys(u2s10);
        driver.findElement(By.id("2-PF_17")).sendKeys(u2s11);
        driver.findElement(By.id("2-PF_18")).sendKeys(u2s12);
        driver.findElement(By.id("2-PF_21")).sendKeys(u2s13);

//Student 3         
        driver.findElement(By.id("3-PF_7")).sendKeys(u3s1);
        driver.findElement(By.id("3-PF_8")).sendKeys(u3s2);
        driver.findElement(By.id("3-PF_9")).sendKeys(u3s3);
        driver.findElement(By.id("3-PF_10")).sendKeys(u3s4);
        driver.findElement(By.id("3-PF_11")).sendKeys(u3s5);
        driver.findElement(By.id("3-PF_12")).sendKeys(u3s6);
        driver.findElement(By.id("3-PF_13")).sendKeys(u3s7);
        driver.findElement(By.id("3-PF_14")).sendKeys(u3s8);
        driver.findElement(By.id("3-PF_15")).sendKeys(u3s9);
        driver.findElement(By.id("3-PF_16")).sendKeys(u3s10);
        driver.findElement(By.id("3-PF_17")).sendKeys(u3s11);
        driver.findElement(By.id("3-PF_18")).sendKeys(u3s12);
        driver.findElement(By.id("3-PF_21")).sendKeys(u3s13);
//Student 4         
        driver.findElement(By.id("4-PF_7")).sendKeys(u4s1);
        driver.findElement(By.id("4-PF_8")).sendKeys(u4s2);
        driver.findElement(By.id("4-PF_9")).sendKeys(u4s3);
        driver.findElement(By.id("4-PF_10")).sendKeys(u4s4);
        driver.findElement(By.id("4-PF_11")).sendKeys(u4s5);
        driver.findElement(By.id("4-PF_12")).sendKeys(u4s6);
        driver.findElement(By.id("4-PF_13")).sendKeys(u4s7);
        driver.findElement(By.id("4-PF_14")).sendKeys(u4s8);
        driver.findElement(By.id("4-PF_15")).sendKeys(u4s9);
        driver.findElement(By.id("4-PF_16")).sendKeys(u4s10);
        driver.findElement(By.id("4-PF_17")).sendKeys(u4s11);
        driver.findElement(By.id("4-PF_18")).sendKeys(u4s12);
        driver.findElement(By.id("4-PF_21")).sendKeys(u4s13);

//Student 5     
        driver.findElement(By.id("5-PF_7")).sendKeys(u5s1);
        driver.findElement(By.id("5-PF_8")).sendKeys(u5s2);
        driver.findElement(By.id("5-PF_9")).sendKeys(u5s3);
        driver.findElement(By.id("5-PF_10")).sendKeys(u5s4);
        driver.findElement(By.id("5-PF_11")).sendKeys(u5s5);
        driver.findElement(By.id("5-PF_12")).sendKeys(u5s6);
        driver.findElement(By.id("5-PF_13")).sendKeys(u5s7);
        driver.findElement(By.id("5-PF_14")).sendKeys(u5s8);
        driver.findElement(By.id("5-PF_15")).sendKeys(u5s9);
        driver.findElement(By.id("5-PF_16")).sendKeys(u5s10);
        driver.findElement(By.id("5-PF_17")).sendKeys(u5s11);
        driver.findElement(By.id("5-PF_18")).sendKeys(u5s12);
        driver.findElement(By.id("5-PF_21")).sendKeys(u5s13);

//Student 6         
        driver.findElement(By.id("6-PF_7")).sendKeys(u6s1);
        driver.findElement(By.id("6-PF_8")).sendKeys(u6s2);
        driver.findElement(By.id("6-PF_9")).sendKeys(u6s3);
        driver.findElement(By.id("6-PF_10")).sendKeys(u6s4);
        driver.findElement(By.id("6-PF_11")).sendKeys(u6s5);
        driver.findElement(By.id("6-PF_12")).sendKeys(u6s6);
        driver.findElement(By.id("6-PF_13")).sendKeys(u6s7);
        driver.findElement(By.id("6-PF_14")).sendKeys(u6s8);
        driver.findElement(By.id("6-PF_15")).sendKeys(u6s9);
        driver.findElement(By.id("6-PF_16")).sendKeys(u6s10);
        driver.findElement(By.id("6-PF_17")).sendKeys(u6s11);
        driver.findElement(By.id("6-PF_18")).sendKeys(u6s12);
        driver.findElement(By.id("6-PF_21")).sendKeys(u6s13);

//Student 7         
        driver.findElement(By.id("7-PF_7")).sendKeys(u7s1);
        driver.findElement(By.id("7-PF_8")).sendKeys(u7s2);
        driver.findElement(By.id("7-PF_9")).sendKeys(u7s3);
        driver.findElement(By.id("7-PF_10")).sendKeys(u7s4);
        driver.findElement(By.id("7-PF_11")).sendKeys(u7s5);
        driver.findElement(By.id("7-PF_12")).sendKeys(u7s6);
        driver.findElement(By.id("7-PF_13")).sendKeys(u7s7);
        driver.findElement(By.id("7-PF_14")).sendKeys(u7s8);
        driver.findElement(By.id("7-PF_15")).sendKeys(u7s9);
        driver.findElement(By.id("7-PF_16")).sendKeys(u7s10);
        driver.findElement(By.id("7-PF_17")).sendKeys(u7s11);
        driver.findElement(By.id("7-PF_18")).sendKeys(u7s12);
        driver.findElement(By.id("7-PF_21")).sendKeys(u7s13);

//Student 8 //      
        driver.findElement(By.id("8-PF_7")).sendKeys(u8s1);
        driver.findElement(By.id("8-PF_8")).sendKeys(u8s2);
        driver.findElement(By.id("8-PF_9")).sendKeys(u8s3);
        driver.findElement(By.id("8-PF_10")).sendKeys(u8s4);
        driver.findElement(By.id("8-PF_11")).sendKeys(u8s5);
        driver.findElement(By.id("8-PF_12")).sendKeys(u8s6);
        driver.findElement(By.id("8-PF_13")).sendKeys(u8s7);
        driver.findElement(By.id("8-PF_14")).sendKeys(u8s8);
        driver.findElement(By.id("8-PF_15")).sendKeys(u8s9);
        driver.findElement(By.id("8-PF_16")).sendKeys(u8s10);
        driver.findElement(By.id("8-PF_17")).sendKeys(u8s11);
        driver.findElement(By.id("8-PF_18")).sendKeys(u8s12);
        driver.findElement(By.id("8-PF_21")).sendKeys(u8s13);   

//Student 9//       
        driver.findElement(By.id("9-PF_7")).sendKeys(u9s1);
        driver.findElement(By.id("9-PF_8")).sendKeys(u9s2);
        driver.findElement(By.id("9-PF_9")).sendKeys(u9s3);
        driver.findElement(By.id("9-PF_10")).sendKeys(u9s4);
        driver.findElement(By.id("9-PF_11")).sendKeys(u9s5);
        driver.findElement(By.id("9-PF_12")).sendKeys(u9s6);
        driver.findElement(By.id("9-PF_13")).sendKeys(u9s7);
        driver.findElement(By.id("9-PF_14")).sendKeys(u9s8);
        driver.findElement(By.id("9-PF_15")).sendKeys(u9s9);
        driver.findElement(By.id("9-PF_16")).sendKeys(u9s10);
        driver.findElement(By.id("9-PF_17")).sendKeys(u9s11);
        driver.findElement(By.id("9-PF_18")).sendKeys(u9s12);
        driver.findElement(By.id("9-PF_21")).sendKeys(u9s13);   


        //Student 10//      
        driver.findElement(By.id("10-PF_7")).sendKeys(u10s1);
        driver.findElement(By.id("10-PF_8")).sendKeys(u10s2);
        driver.findElement(By.id("10-PF_9")).sendKeys(u10s3);
        driver.findElement(By.id("10-PF_10")).sendKeys(u10s4);
        driver.findElement(By.id("10-PF_11")).sendKeys(u10s5);
        driver.findElement(By.id("10-PF_12")).sendKeys(u10s6);
        driver.findElement(By.id("10-PF_13")).sendKeys(u10s7);
        driver.findElement(By.id("10-PF_14")).sendKeys(u10s8);
        driver.findElement(By.id("10-PF_15")).sendKeys(u10s9);
        driver.findElement(By.id("10-PF_16")).sendKeys(u10s10);
        driver.findElement(By.id("10-PF_17")).sendKeys(u10s11);
        driver.findElement(By.id("10-PF_18")).sendKeys(u10s12);
        driver.findElement(By.id("10-PF_21")).sendKeys(u10s13); 


        //Student 11//      
        driver.findElement(By.id("11-PF_7")).sendKeys(u11s1);
        driver.findElement(By.id("11-PF_8")).sendKeys(u11s2);
        driver.findElement(By.id("11-PF_9")).sendKeys(u11s3);
        driver.findElement(By.id("11-PF_10")).sendKeys(u11s4);
        driver.findElement(By.id("11-PF_11")).sendKeys(u11s5);
        driver.findElement(By.id("11-PF_12")).sendKeys(u11s6);
        driver.findElement(By.id("11-PF_13")).sendKeys(u11s7);
        driver.findElement(By.id("11-PF_14")).sendKeys(u11s8);
        driver.findElement(By.id("11-PF_15")).sendKeys(u11s9);
        driver.findElement(By.id("11-PF_16")).sendKeys(u11s10);
        driver.findElement(By.id("11-PF_17")).sendKeys(u11s11);
        driver.findElement(By.id("11-PF_18")).sendKeys(u11s12);
        driver.findElement(By.id("11-PF_21")).sendKeys(u11s13); 


        //Student 12    //      
        driver.findElement(By.id("12-PF_7")).sendKeys(u12s1);
        driver.findElement(By.id("12-PF_8")).sendKeys(u12s2);
        driver.findElement(By.id("12-PF_9")).sendKeys(u12s3);
        driver.findElement(By.id("12-PF_10")).sendKeys(u12s4);
        driver.findElement(By.id("12-PF_11")).sendKeys(u12s5);
        driver.findElement(By.id("12-PF_12")).sendKeys(u12s6);
        driver.findElement(By.id("12-PF_13")).sendKeys(u12s7);
        driver.findElement(By.id("12-PF_14")).sendKeys(u12s8);
        driver.findElement(By.id("12-PF_15")).sendKeys(u12s9);
        driver.findElement(By.id("12-PF_16")).sendKeys(u12s10);
        driver.findElement(By.id("12-PF_17")).sendKeys(u12s11);
        driver.findElement(By.id("12-PF_18")).sendKeys(u12s12);
        driver.findElement(By.id("12-PF_21")).sendKeys(u12s13);

【问题讨论】:

    标签: java excel loops selenium


    【解决方案1】:

    我会尝试使用 DataFile 而不是 DataFormatter:

    DataFile df = new DataFile("D://OneDrive//ABSEC//ATC_Reporting//expdata.xlsx", "Sheet1");
    

    接下来定义你的变量列表(假设是字符串或整数):

    String x = null;
    String y = null;
    String z = null;
    

    Next 循环遍历您的工作表并将数据分配给您的变量。 for 循环假定您有标题。

    for (int i = 1; i < df.getRowCount(); i++) {
      //Assign your variables based on  the column headers
      x = df.getDataFromColumn("Column 1", i);
      y = df.getDataFromColumn("Column 2", i);
      z = df.getDataFromColumn("Column 3", i);
      //now you can use those variables for each row of data
    
    }
    

    根据您上面的代码,您将创建 13 个变量,然后根据需要分配它们。

    此外,您可能需要两个循环,一个学生数组,然后循环遍历 excel 数据以输入该学生的数据。

    String[] students = {"Student 1","Student 2","Student 3"}
    for(String student:students){
      for (int i = 1; i < df.getRowCount(); i++) {
        //Assign your variables based on  the column headers
        x = df.getDataFromColumn("Column 1", i);
        y = df.getDataFromColumn("Column 2", i);
        z = df.getDataFromColumn("Column 3", i);
        //now you can use those variables for each row of data
      }
    

    }

    【讨论】:

    • 太棒了!我很快就会试试这个。快速提问:如果列没有标题,还有其他方法来定义单元格吗?
    • 我认为它们已编入索引,因此您也许可以只使用列索引。您可能希望从 0 而不是 1 开始循环。
    • 如果可能的话,我也对增加元素 ID 的方法感兴趣?我在这里是完全迟钝吗?
    • @DavidS.ID 是动态的还是静态的?如果是静态的,您可以根据行数将整数附加到字符串的末尾......类似于 driver.findElement(By.id("1-PF_"+Integer.toString(i+7))).sendKeys (u1s1);
    • @DavidS。我的回答不正确,它假设你有一个名为数据文件的类,
    【解决方案2】:

    所以我忘记了DataFile类,所以新建一个名为DataFile的类,并添加这些方法如下

    参数:

    private FileInputStream inFile;
    private Workbook wb;
    private Sheet sheet;
    private Hashtable<String, Integer> headers;
    

    数据文件方法:

    public DataFile(String filePath, String sheetName){// throws 
    InvalidFormatException, IOException{
        try {
            //Open the input stream to the file
            inFile = new FileInputStream(filePath);
    
            //Open the workbook from the InputStream and open the Worksheet
            wb = WorkbookFactory.create(inFile);
            sheet = wb.getSheet(sheetName);
    
            //Keep track of the column headers and their indexes
            headers = new Hashtable<String, Integer>();
            Row row = sheet.getRow(0);
            for(int col=0; col < row.getLastCellNum(); col++){
                String curr = row.getCell(col).getStringCellValue();
                headers.put(curr, new Integer(col));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    获取数据列方法:

    public String getDataFromColumn(String columnHeader, int rowNumber) {
            //Get the column number
            Integer colNum = headers.get(columnHeader);
            if (colNum == null){
                return null;
            } else if (rowNumber > sheet.getLastRowNum()){
                return null;
            }
    
            //Return the data from the specified cell
            Cell cell = sheet.getRow(rowNumber).getCell(colNum.intValue());
    
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                return cell.getStringCellValue();
            case Cell.CELL_TYPE_NUMERIC:
                return Double.toString(cell.getNumericCellValue()); 
            case Cell.CELL_TYPE_BOOLEAN:
                return Boolean.toString(cell.getBooleanCellValue());
            case Cell.CELL_TYPE_BLANK:
                return "";  
            case Cell.CELL_TYPE_FORMULA:
                FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
    
                switch (eval.evaluateFormulaCell(cell)) {
                case Cell.CELL_TYPE_STRING:
                    return cell.getStringCellValue();
                case Cell.CELL_TYPE_NUMERIC:
                    return Double.toString(cell.getNumericCellValue());
                case Cell.CELL_TYPE_BOOLEAN:
                    return Boolean.toString(cell.getBooleanCellValue());
                case Cell.CELL_TYPE_BLANK:
                    return "";
    
                default:
                    break;
                }
            }
    
            return null;
        }
    

    获取行数(如果没有标题,则包括标题修改)

    public int getRowCount() {
        return this.sheet.getLastRowNum()+1;
    

    关闭文件

    public void close() throws IOException {
            this.inFile.close();
        }
    

    如果你使用的是 maven,请将以下依赖项添加到你的 pom 文件中:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.11</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.11</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.11</version>
    </dependency>
    

    【讨论】:

    • 非常感谢!我非常感谢您回来并为我提供了使这项工作正常进行所需的一切。
    • 我很抱歉不知道这个问题的答案,但是当我创建 DataFile 类并粘贴参数后面的方法时,这是否应该起作用?我看到一堆错误,不知道如何把它们放在一起。
    • @DavidS。你用的是maven吗?我编辑了答案以包含正确的依赖项。
    • 我正在使用日食氧气......我应该改变吗?我现在没有任何束缚
    • @DavidS。你的 Eclipse 项目有 pom.xml 文件吗?如果是这样,请在此处添加依赖项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多