如何从excel读取多个数据并使用selenium webdriver

时间:2018-03-28 20:37:56

标签: excel selenium-webdriver

我需要从excel文件的20列中读取数据,然后使用selenium web-driver将其输入到文本框中。数据应以逗号分隔。

excel中的测试数据:

A
B
C

以下是我编写的代码,但我无法用逗号分隔数据

package newpackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

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.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Add_Data {
    public static void main(String[] args) throws InterruptedException, IOException {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Dinesh\\Downloads\\ChromeDriver\\chromedriver_win32\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");
        options.addArguments("--start-maximized");

        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.example.com");

        //enter username
        WebElement username = driver.findElement(By.id("Login"));
        username.sendKeys("test");

        //enter password
        WebElement password = driver.findElement(By.id("Passwd"));
        password.sendKeys("password");

        WebElement login = driver.findElement(By.name("action"));
        login.click();

        Thread.sleep(30000);

        driver.findElement(By.xpath("//*[@id='textbox']")).click();

        try {
            File src= new File("C:\\ExcelData\\TestData.xlsx");

            FileInputStream fis=new FileInputStream(src);

            XSSFWorkbook wb=new XSSFWorkbook(fis);

            XSSFSheet Sheet1=wb.getSheetAt(0);

            int rowcount=Sheet1.getLastCellNum();

            for(int i=0;i<rowcount+1;i++) {

            String data0=Sheet1.getRow(i).getCell(0).getStringCellValue();   

            //print the value from excel to textbox and split the data in textbox 
            driver.findElement(By.xpath("//[@id='textbox']")).sendKeys(split(data0,rowcount));

            wb.close();
            //close of try catch block    
            }
        } catch (IOException e) {
        }
    }
}

输出:我可以将文本框中的值打印为ABC而不是A,B,C

0 个答案:

没有答案