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?

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;
}
}
Mehmet FatihPosted Jan 16, 2024, 8:17 AM
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]"));
Jayraj ChhayaPosted Jan 16, 2024, 7:41 AM
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:
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 FatihPosted Jan 16, 2024, 6:11 AM
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]")
Naimish MakwanaPosted Jan 16, 2024, 5:25 AM
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: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
Prasad RaveendranPosted Jan 16, 2024, 3:41 AM
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:
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.