2014-11-04 77 views
-2

這裏的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(); 
    } 

回答

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

感謝您的幫助 – 2014-11-04 17:01:07

1

把字符串成數組並遍歷數組。

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

感謝您對我的幫助 – 2014-11-04 17:01:49