I transfer student grades in Datagridview to the web page with Selenium. However, when the student order on the web page and the student order in the datagridview are different, each student's grade is transferred differently because the grades are transferred sequentially. How can I transfer grades according to student number, using student number as a criterion?

C#
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
try
{
var val0 = dataGridView1.Rows[i].Cells[0].Value?.ToString();// Ö.NO
if (val0 != null && val0 != string.Empty)
{
var yaz1 = dataGridView1.Rows[i].Cells[3].Value.ToString();
IWebElement element = drv.FindElement(By.Id("grade1" + (i)))
element.Clear();
element.SendKeys(yaz1);
}
}
catch (NoSuchElementException)
{
continue;
}
}
Prasad RaveendranRank #320 · 7.8K reputationPosted Jan 15, 2024, 10:41 PM EST
To transfer grades according to the student number, you should first find the correct order of students on the web page based on their student numbers. Then, you can iterate through the DataGridView using the student numbers as a criterion and find the corresponding elements on the web page.
Here's a modified version of your code to achieve this:
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { try { var studentNumber = dataGridView1.Rows[i].Cells[0].Value?.ToString(); if (studentNumber != null && studentNumber != string.Empty) { // Find the index of the student on the web page based on the student number int webPageIndex = FindStudentIndexOnWebPage(driver, studentNumber); if (webPageIndex != -1) { var grade = dataGridView1.Rows[i].Cells[3].Value.ToString(); IWebElement element = driver.FindElement(By.Id("grade1" + webPageIndex)); element.Clear(); element.SendKeys(grade); } else { // Handle the case where the student is not found on the web page Console.WriteLine($"Student with number {studentNumber} not found on the web page."); } } } catch (NoSuchElementException) { continue; } } // Function to find the index of a student on the web page based on their student number private int FindStudentIndexOnWebPage(IWebDriver driver, string studentNumber) { // You need to implement the logic to find the correct index based on the student number // This may involve searching elements on the web page and comparing student numbers // Return the index if found, or -1 if not found // Example: // IWebElement studentElement = driver.FindElement(By.XPath($"//*[contains(text(), '{studentNumber}')]")); // int index = /* logic to get the index */; // return index; }In the
FindStudentIndexOnWebPagefunction, you need to implement the logic to find the correct index based on the student number. This may involve searching elements on the web page and comparing student numbers. The example uses XPath as an illustration, but you may need to adapt it based on the structure of your web page.Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 16, 2024, 12:25 AM EST
It seems like you’re trying to match the student number from your DataGridView with the student number on the web page, and then transfer the grades. In your current code, you’re using the index
ito find the element on the web page, which won’t work if the order of students in the DataGridView and the web page are not the same.You can modify your code to use the student number (
val0) to find the corresponding element on the web page. Here’s a modified version of your code:for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { try { var val0 = dataGridView1.Rows[i].Cells[0].Value?.ToString(); // Student number if (val0 != null && val0 != string.Empty) { var yaz1 = dataGridView1.Rows[i].Cells[3].Value.ToString(); // Grade IWebElement element = drv.FindElement(By.Id("grade" + val0)); // Use student number to find the element element.Clear(); element.SendKeys(yaz1); } } catch (NoSuchElementException) { continue; } }In this code,
"grade" + val0should correspond to the ID of the grade input field for the student with the numberval0on the web page. Please replace"grade"with the correct prefix if it’s different on your web page. This code assumes that each student has a unique ID on the web page that can be derived from their student number.Thanks
Mehmet FatihRank #1,474 · 1.1K reputationPosted Jan 16, 2024, 1:11 AM EST
Thank you both. I tried both your codes. I am getting the error on word FindStudentIndexOnWebPage in Prasad code.I can't use private int FindStudentIndexOnWebPage(IWebDriver driver, string studentNumber) because It depends on button click event. In Naimish's code, it neither gives an error nor fills in the exam notes. I'm sorry, but when filling out the exam grades, the input value increases by one in each line. so have to use "grade1" + (i))
IWebElement element = drv.FindElement(By.Id("grade1" + (i)));.
on web page number xpath adress is that
drv.FindElement(By.XPath("/html/body/form[2]/table/tbody/tr[3]/td[2]/table/tbody/tr[3]/td/table/tbody/tr[9]/td[2]/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[2]/td/table/tbody/tr[2]/td[1]")
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 16, 2024, 2:41 AM EST
To transfer grades according to the student number, you can modify your code to match the student number from the DataGridView with the corresponding element on the web page. Here's an example of how you can achieve this:
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { try { var studentNumber = dataGridView1.Rows[i].Cells[0].Value?.ToString(); // Get the student number from the DataGridView if (!string.IsNullOrEmpty(studentNumber)) { var grade = dataGridView1.Rows[i].Cells[3].Value.ToString(); // Get the grade from the DataGridView // Find the element on the web page using the student number as a criterion IWebElement element = drv.FindElement(By.Id("grade1" + studentNumber)); element.Clear(); element.SendKeys(grade); } } catch (NoSuchElementException) { continue; } }In this modified code, we retrieve the student number from the DataGridView using
dataGridView1.Rows[i].Cells[0].Value?.ToString(). Then, we find the corresponding element on the web page usingdrv.FindElement(By.Id("grade1" + studentNumber)), where"grade1"is the prefix for the element ID on the web page.Mehmet FatihRank #1,474 · 1.1K reputationPosted Jan 16, 2024, 3:17 AM EST
Thanks Jayraj but it is not working. At the same time my grade imputs increase one by one in each line so I have to use my code like that
IWebElement element = drv.FindElement(By.Id("grade" + (i) + studentNumber));
At the same time the number xpath adres on web page is like that
IWebElement element = drv.FindElement(By.XPath("/html/body/form[2]/table/tbody/tr[3]/td[2]/table/tbody/tr[3]/td/table/tbody/tr[9]/td[2]/table/tbody/tr/td/table/tbody/tr[2]/td/table/tbody/tr[2]/td/table/tbody/tr["+(i+2)+"]/td[1]"));