如何为以下功能创建循环

时间:2014-11-04 13:52:46

标签: javascript selenium-webdriver

这里webdriver应该点击每个元素,但行动是一样的,你能帮助我吗? 我是JavaScript的新手

public void clickOnAllFiters(){
        driver.findElement(By.cssSelector("div.scroll-item.all")).click();
        driver.findElement(By.cssSelector("div.scroll-item.news")).click();
        driver.findElement(By.cssSelector("div.scroll-item.results")).click();
        driver.findElement(By.cssSelector("div.scroll-item.schedules")).click();
        driver.findElement(By.cssSelector("div.scroll-item.images")).click();
        driver.findElement(By.cssSelector("div.scroll-item.video")).click();
        driver.findElement(By.cssSelector("div.scroll-item.comment")).click();
        driver.findElement(By.cssSelector("div.scroll-item.activity")).click();
    }

2 个答案:

答案 0 :(得分:1)

clickOnAllFilters("div.scroll-item.activity", "div.scroll-item.activity"); // You can add more, or simply send an array of strings.

public void clickOnAllFilters(String... filters)
{
    for(String filter : filters)
    {
        driver.findElement(By.cssSelector(filter)).click();
    }
}

答案 1 :(得分:1)

将字符串放入数组并循环遍历数组。

String[] strings = new String[] {"div.scroll-item.all", ...};
for (String string : strings) {
  driver.findElement(By.cssSelector(string)).click();
}