2014-12-03 15 views
1

Selecting relational highchart SVG images with Selenium如何循環瀏覽webdriver中的條形圖元素?

最近我問了上面的問題,我想點擊一個高圖表中的一些元素,以便在我的自動化測試中完成一些功能。我現在已經解決了這個問題,但還有一個問題。在某些情況下,我會在高圖上有3個小節;在其他人中我可能會有更多;或更少。我寫的代碼,很明顯,是因爲它不能動態處理,如果有變化,以高圖表的數頁上的限制(我已經寫了下面的代碼):

public static void barChartSelector(InternetExplorerDriver driver) 
{ 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    WebElement parent = driver.findElement(By.className("highcharts-series-group")); 
    List<WebElement> children = parent.findElements(By.tagName("rect")); 
    children.get(0).click(); 
    children.get(1).click(); 
    children.get(2).click(); 
    children.get(3).click(); 
} 

我在概念上,我們知道需要進行某種循環,因此如果我在一個有4個元素的高圖上使用barChartSelector方法來點擊,該方法可以遍歷並處理它。同樣,如果有另一個只有3個元素的高圖,代碼也應該處理這個。這隻會使代碼更具動態性和麪向未來。

有沒有人有任何建議的最佳方式來實現上述?

回答

2

也許代替

List<WebElement> children = parent.findElements(By.tagName("rect")); 
    children.get(0).click(); 
    children.get(1).click(); 
    children.get(2).click(); 
    children.get(3).click(); 

你可以嘗試

List<WebElement> children = parent.findElements(By.tagName("rect")); 
for (WebElement cur : children) { 
    cur.click(); 
} 
+0

非常感謝克里斯蒂安,我會給予一個着呢 – 2014-12-03 09:31:18

+0

該解決方案已經非常完美,非常感謝! – 2014-12-03 10:03:32