I have a Selenium WebDriver script written in C# that reads some values from an Excel spreadsheet and then uses the values from the row to fill a web form. The challenge I have now is that it takes the first cell value from the Excel file and enters it to all the fields in the form, then takes the next value and does the same and so on.
How can I make it such that it takes the 1st value, add to the first(named field) in the form, takes 2nd value and to 2nd named field and so on.
Please see code for method below.
public void FillForm()
//Function reads entries from an Excel spreadsheet then uses values to populate and fill the form
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range xlrange;
string xlString;
int xlRowCnt = 0;
int xlColCnt = 0;
xlApp = new Excel.Application();
//Open Excel file
xlWorkBook = xlApp.Workbooks.Open(@"D:\Projects\Data\MSI_Data_file.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//This gives the used cells in the sheet
xlrange = xlWorkSheet.UsedRange;
for (xlRowCnt = 1; xlRowCnt <= xlrange.Rows.Count; xlRowCnt++)
{
for (xlColCnt = 1; xlColCnt <= xlrange.Columns.Count; xlColCnt++)
{
xlString = (string)(xlrange.Cells[xlRowCnt, xlColCnt] as Excel.Range).Value2;
driver.FindElement(By.XPath("//input[contains(@name, 'FirmName')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//input[contains(@name, 'FirstName')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//input[contains(@name, 'LastName')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//input[contains(@name, 'Email')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//input[contains(@name, 'FirmAddress')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//select[@id= 'ddlCountry']")).SendKeys(xlString);
driver.FindElement(By.XPath("//input[contains(@name, 'PhoneNumber')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//input[contains(@name, 'FaxNumber')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//input[contains(@name, 'Website')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//textarea[contains(@name, 'Comments')]")).SendKeys(xlString);
driver.FindElement(By.XPath("//input[@id='chkFirm_Service_Accounting']")).Click();
driver.FindElement(By.XPath("//select[contains(@name, 'LeadSource')]")).SendKeys(xlString);
//save screenshot of completed form
SaveScreenShot("CompleteForm");
driver.FindElement(By.XPath("//a[contains(text(), 'Submit')]")).Click();
//Take screenshot of successful form submission
SaveScreenShot("Submission_Success");
driver.FindElement(By.XPath("//a[contains(text(), 'click here')]")).Click();
}
}
}