当前位置: 动力学知识库 > 问答 > 编程问答 >

Selenium looping with spans

问题描述:

i am new with selenium. I want to make a loop with spans. It should start with A character. 0-9 shouldnt be got in loop.

<div id="alpha">

<div class="alphabets">

<span data-value="0-9" class="alphabetSearch">0-9</span>

<span data-value="A" class="alphabetSearch active">A</span>

<span data-value="B" class="alphabetSearch">B</span>

<span data-value="C" class="alphabetSearch">C</span>

<span data-value="Ç" class="alphabetSearch">Ç</span>

<span data-value="D" class="alphabetSearch">D</span>

</div>

</div>

网友答案:

i am just providing some logic in java here.. i used xpath to collect required span elements, you can use any other appropriate locator.

//in java
List<WebElement> allalphabets=driver.findElements(By.xpath("//*[@id='alpha']/div/span"));

  //starting from j=1, as 0 is for '0-9' which should not be in loop
  for(int j=1; j<allalphabets.size();j++){

      //do your logic here
      //just to print text
      System.out.println(allalphabets.get(j).getText());
  }
网友答案:

try the following code:

int i = 1;   
List<WebElement> allspan= driver.findElements(By.cssSelector("#alpha>div>span"));

for (WebElement spanvalues: allspan) {

    if(spanvalues.getText().equals("0-9")){ 

                continue;
    }       

else
//do your code


i++;    
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: