Selenium - 从excel中提取数据并写入网页 - 在程序中查询

时间:2014-12-11 09:46:17

标签: excel selenium

我正在尝试从excel中提取数据并将其写入网页。当我点击下拉菜单时,我就是"在下面的代码中的链接中提供了页面的其余内容更改。我已将某些单元格设置为空白。我编写了代码但得到了一些例外。请帮助

import java.io.*;
import java.util.concurrent.TimeUnit;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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.support.ui.Select;

public class Copyprogram {
    public static WebDriver driver=new FirefoxDriver();

    public static void main(String[] args)

    {
        driver.get("http://www.deal4loans.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("html/body/div[4]/div/div/div[1]/a[1]")).click();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        try {

                FileInputStream fis = new FileInputStream("C:\\Users\\user\\Documents\\Copy of  Booklet1.xlsx");

            XSSFWorkbook wb = new XSSFWorkbook(fis);
                  XSSFSheet sheet = wb.getSheet("testdata");
            // Loop through all rows in the sheet
            // Start at row 1 as row 0 is our header row

       int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
            for(int i = 1;i<=rowCount;i++)
                {

                 Row row1 = sheet.getRow(i);

            try {
                Thread.sleep(2000);
                } catch (Exception e) 
                {
                // TODO: handle exception
                }

                runTest(row1.getCell(1).toString(),row1.getCell(2).toString(),row1.getCell(3).toString(),row1.getCell(4).toString(),row1.getCell(5).toString(),row1.getCell(6).toString(),row1.getCell(7).toString(),row1.getCell(8).toString(),row1.getCell(9).toString(),row1.getCell(10).toString(),row1.getCell(11).toString(),row1.getCell(12).toString(),row1.getCell(13).toString() );

                System.out.println(row1.getCell(1).toString());
                System.out.println(row1.getCell(2).toString());
                System.out.println(row1.getCell(3).toString());
                System.out.println(row1.getCell(4).toString());
                System.out.println(row1.getCell(5).toString());
                System.out.println(row1.getCell(6).toString());
                System.out.println(row1.getCell(7).toString());
                System.out.println(row1.getCell(8).toString());
                System.out.println(row1.getCell(9).toString());
                System.out.println(row1.getCell(10).toString());

               if (i<rowCount)
               {
                driver.navigate().back();
            }

                System.out.println(i);
                }
            fis.close();
        } catch (IOException e) {
            System.out.println("Test data file not found");
        }   

        driver.close();

    }

    public static  void runTest(String name,String mailid,String query,String city,String mob,String pdt,String PL,String HL,String CL,String LA,String BL,String CC,String ass) 
    {

    driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/input")).sendKeys(name);

    driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).sendKeys(mailid);

    Select listbox1 = new Select(driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[3]/td[2]/select")));
      listbox1.selectByValue(query);

      String selectedValue =listbox1.getFirstSelectedOption().getText();

           Select listbox2 = new Select(driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[4]/td[2]/select")));
       listbox2.selectByValue(city);

      driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[6]/td[2]/input")).sendKeys(mob);
     if (selectedValue=="1")
     {

       Select listbox3 = new Select(driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[7]/td/div[2]/table/tbody/tr/td[2]/select")));
       listbox3.selectByValue(pdt);

     }

     else

     {
         if (PL == null && PL.length() == 0)
         {
      driver.findElement(By.cssSelector("input[value=PL]")).click();
      System.out.println(PL);
         }


        if(HL == null && HL.length() == 0)
        {

      driver.findElement(By.cssSelector("input[value=HL]")).click();

        }

        if(CL == null && CL.length() == 0)
        {

      driver.findElement(By.cssSelector("input[value=CL]")).click();
        }
        if(LA == null && LA.length() == 0)
        {
      driver.findElement(By.cssSelector("input[value=LA]")).click();
        }
        if(BL == null && BL.length() == 0)
        {

      driver.findElement(By.cssSelector("input[value=BL]")).click();

        }

        if(CC== null && CC.length() == 0)
        {
      driver.findElement(By.cssSelector("input[value=CC]")).click();
     }
    driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[7]/td/div[3]/table/tbody/tr[2]/td[2]/input")).sendKeys(ass);


     }
    driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[9]/td/input")).click();

    }
    }

0 个答案:

没有答案