Java - Selenium Web 驱动程序:从 Excel 中提取日期格式 MM/DD/YY 并填充到 Web 字段中,导致月份和日期相互切换

Java - Selenium Web Driver: Pulling date format MM/DD/YY from Excel and populating in the web field cause switch month and day to each other place

我正在尝试从 Microsoft Excel 97-2003 Worksheet 中提取日期格式 MM/DD/YYYY 并填充到 Web 字段中,导致月份和日期相互切换导致脚本失败。

我想在网络字段中以 MM/DD/YYYY 格式(美国格式)填充日期。但是,在网络字段中,日期填充为 DD/MM/YYYY.

我 excel sheet 中的以下日期格式:

10/18/1976

当日期填充到网络字段中时,它会按如下方式填充:

18/10/1976

以下是我的代码:

package com.Test

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TC20042 extends BaseClass{

 @Test
        public void registration() throws Exception, InterruptedException {
              FileInputStream fi=new FileInputStream("C:\File\Book2.xls");
              Workbook w=Workbook.getWorkbook(fi);
              Sheet s=w.getSheet(2);


            try
            {
            for (int i = 0; i < s.getRows(); i++)
            {
            //Read data from excel sheet

                String s7 = s.getCell(6,i).getContents();

driver.findElement(By.xpath("//*[@id='__o3id9']")).sendKeys(s7);
                Thread.sleep(500);

            }

            }
            catch(Exception e)
            {
            System.out.println(e);

            }
            }
}

我刚刚注意到当我在 excel sheet 中输入测试数据时,我在 excel sheet 中使用了默认日期格式,如下所示:

但后来我意识到可能在单元格格式选项中有另一种自定义日期格式,我更新如下并且现在对我有用: