如何在java中使用SeleniumWebDriver在excel的webform(与任何注册页面一样)上设置值?

如何在java中使用SeleniumWebDriver在excel的webform(与任何注册页面一样)上设置值?,java,excel,selenium,webdriver,Java,Excel,Selenium,Webdriver,这是更新的代码 现在我试着阅读并把它放在网络表单上,但不知何故这两件事都不起作用。现在,我不知道如何使用selenium webdriver在webform上放置值。我已经在代码中放置了逻辑,但到期文件未发现其未正确获取。逻辑是否正确。请帮助我 提前谢谢 package testdata; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Itera

这是更新的代码 现在我试着阅读并把它放在网络表单上,但不知何故这两件事都不起作用。现在,我不知道如何使用selenium webdriver在webform上放置值。我已经在代码中放置了逻辑,但到期文件未发现其未正确获取。逻辑是否正确。请帮助我 提前谢谢

package testdata;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.apache.poi.ss.usermodel.Cell;
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.firefox.FirefoxDriver;

public class DatawithExcel { public static void main(String[] args) throws InterruptedException  {   
            FirefoxDriver driver = new FirefoxDriver();

            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

            driver.get("http://newtours.demoaut.com/mercurywelcome.php?osCsid=670caec15c0566144c663f12af6fa66b");

            driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a")).click();
            try {
                FileInputStream fis = new FileInputStream("C:\\testdata.xlsx");
                XSSFWorkbook wb = new XSSFWorkbook(fis);

                XSSFSheet sheet = wb.getSheet("TestData");

                for(int count=1;count<=sheet.getLastRowNum();count++)
                {
                    XSSFRow row = sheet.getRow(count);
                    System.out.println("\n----------------------------");
                    System.out.println("Running test case " + count);

                 runTest(row.getCell(0).toString(),row.getCell(1).toString(),row.getCell(2).toString(),row.getCell(3).toString(), row.getCell(4).toString(),row.getCell(5).toString(),row.getCell(6).toString());

                }
                fis.close();
                driver.close();// Closing the firefox driver instance
            } catch (IOException e) {
                System.out.println("Test data file not found");
            }   
        }

         public static  void runTest(String FN,String LN,String Phone,String userName,String Address,String city,String State ) throws InterruptedException 
            {       
                System.out.println("Inputing First name: "+FN+"  Last Name:"+LN+" Phone: "+Phone+"  Email: "+userName+"Address:"+Address+"city"+city+"State:"+State+"");
                FirefoxDriver driver = new FirefoxDriver();
            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")).clear();
                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")).clear();
                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")).clear();
                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")).clear();
                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")).clear();
                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")).clear();
                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")).clear();
                driver.findElement( By.name("firstName")).sendKeys(FN);
                driver.findElement( By.name("lastName")).sendKeys(LN);
                driver.findElement( By.name("phone")).sendKeys(Phone);
                driver.findElement( By.name("userName")).sendKeys(userName);
                driver.findElement( By.name("address1")).sendKeys(Phone);
                driver.findElement( By.name("city")).sendKeys(Address);
                System.out.println("Inputing First name: "+FN+"  Last Name:"+LN+"Phone: "+Phone+"  Email: "+userName+"Address:"+city+"State:"+State+"");

                Thread.sleep(2000); // Sleeping 2 seconds so that each entry is detected.

            }
        } 

您可能想看看下面实现的类,目的相同。可能重复的
successfully executed both the parts reading from xcel and putting on to webpage thanks for ur helpful suggestions make this possible.now both things are working.  

  import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.concurrent.TimeUnit;

    import org.apache.poi.xssf.usermodel.XSSFCell;
    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.firefox.FirefoxDriver;
    import driverMain.ReadExcel;


    public class testNewTour {

        public static WebDriver driver = null;
         public static void main(String[] args) throws InterruptedException, IOException
            {   


                driver = new FirefoxDriver();

                driver.manage().window().maximize();
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

                driver.get("http://newtours.demoaut.com/mercurywelcome.php?osCsid=670caec15c0566144c663f12af6fa66b");

                driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a")).click();

                FileInputStream fis = new FileInputStream("C:\\Ruchi\\TestData.xlsx");
                XSSFWorkbook wb = new XSSFWorkbook(fis);
                XSSFSheet ws = wb.getSheet("Sheet1");

               XSSFRow dataRow = ws.getRow(1);
                      XSSFCell r3c2 = dataRow.getCell(0);
                      String FirstName = r3c2.getStringCellValue();


                      System.out.println("FirstName: " + FirstName);

                      for(int count=1;count<=ws.getLastRowNum();count++)
                        {
                            XSSFRow row = ws.getRow(count);
                            System.out.println("\n----------------------------");
                            System.out.println("Running test case " + count);

                            runTest(row.getCell(0).toString(),row.getCell(1).toString(),row.getCell(2).toString(),row.getCell(3).toString(), row.getCell(4).toString(),row.getCell(5).toString(),row.getCell(6).toString());

                        }


                    fis.close();
                    driver.close();// Closing the firefox driver instance
                } 


             public static  void runTest(String FN,String LN,String Phone,String userName,String Address,String city,String State ) throws InterruptedException 
                {       
                    System.out.println("Inputing First name: "+FN+"  Last Name:"+LN+" Phone: "+Phone+"  Email: "+userName+"Address:"+Address+"city"+city+"State:"+State+"");

                    driver.findElement( By.name("firstName")).sendKeys(FN);
                    driver.findElement( By.name("lastName")).sendKeys(LN);
                    driver.findElement( By.name("phone")).sendKeys(Phone);
                    driver.findElement( By.name("userName")).sendKeys(userName);
                    driver.findElement( By.name("address1")).sendKeys(Address);
                    driver.findElement( By.name("city")).sendKeys(city);
                    driver.findElement( By.name("state")).sendKeys(State);



                    System.out.println("Inputing First name: "+FN+"  Last Name:"+LN+"Phone: "+Phone+"  Email: "+userName+"Address:"+city+"State:"+State+"");

                    Thread.sleep(7000); // Sleeping 2 seconds so that each entry is detected.
    //
                }
            }