2013-05-25 41 views
0

我想單擊表中的多個項目。我通過按住Ctrl鍵並使用xpath單擊多個鏈接來完成此操作。現在點擊多個xpath我while循環多少鏈接我想點擊。但我不能這樣做,在一個行動在Java中執行。 這裏是顯示錯誤將Action循環放在Action中執行在Java Webdriver中不起作用?

new Actions(driver).keyDown(Keys.CONTROL). //syntax error on token".", ; expected. 
while(items > 0) 
{ 
click(driver.findElement(By.xpath("//div/div/div/div[`$items`]/div/div"))). //click(Webelement is undefined 
} 
keyUp(Keys.CONTROL). //keyUP is undefined 
perform(); 

我評論過我得到的相應行中的錯誤代碼。如果刪除while循環,它工作正常。只有在添加while循環時纔會出現問題。請幫忙

+0

爲什麼你喜歡選擇表中的項目?我認爲這只是UI幫助用戶在表格中製作更好的副本(您正在使用Firefox吧?)。 –

回答

1

我的建議是,你可能想在做這個之前有一些Java編程教程。這不需要很長時間,但是你可能會花幾個小時試圖弄清楚一些語法錯誤。

我相信你複製該代碼別的地方

new Actions(driver).keyDown(Keys.CONTROL).click().keyUp(Keys.CONTROL).perform(); 

你的邏輯主要是正確的,但你可知道,Java語句結束了;而不是.,這是什麼錯誤消息「語法錯誤令牌「。」,「預計」。手段。

如果您註釋掉while循環,則代碼與上面的代碼相同,因爲它是一個整體聲明,以;結尾。所以不會有任何語法錯誤。

new Actions(driver).keyDown(Keys.CONTROL). 
// whatever in the while loop 
keyUp(Keys.CONTROL). 
perform(); 

當您添加在while循環,你要的是一個說法壓下Keys.CONTROL,然後while循環點擊,那麼語句釋放控制。你不能只把while循環放到一個語句中。

new Actions(driver).keyDown(Keys.CONTROL).perform(); // end with semicolon 
while(items > 0) 
{ 
// wrong while loop logic, items will never change in the loop and what's $items? 
// do you want a for loop with index? I don't think this locator is valid. 
// however, the syntax error should be fixed. 
new Actions(driver).click(By.xpath("//div/div/div/div[`$items`]/div/div")).perform(); // end with semicolon 
} 
new Actions(driver).keyUp(Keys.CONTROL).perform(); // end with semicolon 

一旦你擺脫了錯誤,我們可以繼續看看它實際工作與否。