Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using selenim with C#. I want to increase the div number one by one with loop each time. I am trying the following codes by beginning the second line. But it doesn't work. What is the solution? The line with problem is div[2+i] i doesn't loop.

What I have tried:

C#
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();        
           }
Posted
Updated 5-Nov-23 0:44am
v4

A variable name inside a string has no meaning. In the following line the characters "i+2" are merely part of the string, and will be treated as such.
C#
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();

If you want to use an evaluated expression then you need to use a string formatting option:
C#
driver.FindElement(By.XPath(string.Format("/html/body/app-root/app-auth/app-public/app-card/div/div/div[6]/div[{0}]/div/div[1]/kendo-datepicker/button/span", i + 2)).Click();
 
Share this answer
 
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:
C#
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();
}
 
Share this answer
 
Comments
Member 12505620 28-Oct-23 16:53pm    
Thanks Mohammed Imran Ansari. It works fine.
M Imran Ansari 28-Oct-23 18:24pm    
You're welcome! I'm glad to hear that the solution worked for you.
Happy Coding!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900