多个For-Loops

Nic*_*ahn 0 c# for-loop

编辑:我已经更新了我的问题,我知道我可以增加循环而不是4到12,但我之所以分开三个for循环的原因是由于这个原因; 在迭代第一个循环后,我必须单击轮播以进入下一个下一个迭代,因为您可以看到我更新的代码. - 对不起,不知怎的,我在复制/粘贴时错过了代码.

我想迭代遍历行,我拥有的总行数为3,每行有4列,例如:

row 1 has 4 columns
row 2 has 4 columns
row 3 has 4 columns
Run Code Online (Sandbox Code Playgroud)

我创建了3个单独的for循环,我的代码可以工作,但我相信有改进我的代码的空间,它可以在一个循环而不是三个?

这是代码:

//first row

for (int j = 0; j < 4; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();
}

_carouselDot = String.Format("id('HomePage1_Carousel1')/div/ul/li[{0}]/a", 2);
WaitForElement(By.XPath(_carouselDot)).Click();

//second row
for (int j = 4; j < 8; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();
}

_carouselDot = String.Format("id('HomePage1_Carousel1')/div/ul/li[{0}]/a", 3);
WaitForElement(By.XPath(_carouselDot)).Click();

//third row
for (int j = 8; j < 12; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

是的,你可以有一个从0到12的循环...

for (int j = 0; j < 12; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();
}
Run Code Online (Sandbox Code Playgroud)


Aar*_*ron 5

像这样?

for (int i = 0; i < 3; i++) 
{  
   for (int j = 0; j < 4; j++)
   { 
        code here
   }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

因为你编辑了你的问题,现在看起来好多了:

for (int i = 0; i < 12; i++)
       { 
            string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
            string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
            ...........
            WaitForElement(By.XPath(_text)).Click();

            if (i % 4 == 0)
            {
                _carouselDot = String.Format("id('HomePage1_Carousel1')/div/ul/li[{0}]/a", 2);
                WaitForElement(By.XPath(_carouselDot)).Click();
            }
       }
Run Code Online (Sandbox Code Playgroud)