I am using selenim with c#. I want to increase the div number one by one each time. I am trying the following codes by beginning the second line.but it doesn't work What is the solution?
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
var val1 = dataGridView1.Rows[i].Cells[3].Value.ToString().;
driver.FindElement(By.XPath("/html/body/app-root/app-auth/app-public/app-card/div/div/div[6]")).Click();
driver.FindElement(By.XPath("/html/body/app-root/app-auth/app-public/app-card/div/div/div[6]/div[i+2]/div/div[1]/kendo-datepicker/button/span")).Click();
Thread.Sleep(100);
driver.FindElement(By.XPath("/html/body/app-root/app-auth/app-public/app-card/div/div/div[6]/div[i+2]/div/div[1]/kendo-datepicker/button/span")).Click();
}

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 28, 2023, 10:37 AM EDT
There are a couple of issues in your code. Firstly, you have an extra dot after the ToString() method call, remove dot from the last. Secondly, the usage of the index i inside the XPath might not work as intended. Here is a revised version of your code with proper string concatenation using string interpolation to dynamically inject the value of i into the XPath expression:
for (int i = 0; i < dataGridView1.Rows.Count; i++) { var val1 = dataGridView1.Rows[i].Cells[3].Value.ToString(); driver.FindElement(By.XPath("/html/body/app-root/app-auth/app-public/app-card/div/div/div[6]")).Click(); driver.FindElement(By.XPath($"/html/body/app-root/app-auth/app-public/app-card/div/div/div[6]/div[{i+2}]/div/div[1]/kendo-datepicker/button/span")).Click(); Thread.Sleep(100); driver.FindElement(By.XPath($"/html/body/app-root/app-auth/app-public/app-card/div/div/div[6]/div[{i+2}]/div/div[1]/kendo-datepicker/button/span")).Click(); }Mehmet FatihRank #1,475 · 1.1K reputationPosted Oct 28, 2023, 4:46 PM EDT
Thanks Muhammad Imran Ansari, it works fine. I am gratefull to you. Allah bless you.
Sam HobbsRank #107 · 20.5K reputationPosted Oct 28, 2023, 12:48 PM EDT
I see you are finding the path:
Multiple times. If it were me, I would find it once and then ensure it was valid then use the result multiple times.
You are using the result of each
XPathquery without checking whether the result is valid. It would be better to ensure a path exists before using it, such as to callClick.