2017-06-18 82 views
-1

我嘗試使用下面的代碼來獲取所有從下拉菜單中的元素:硒的webdriver findelements能夠gettext的元素,但不能點擊

List<WebElement> actmenu = driver.findElements(By.className("mbrMenuItems")); 
    for (int i = 0; i < actmenu.size(); i++) { 
     System.out.println(actmenu.get(i).getText()); 
    } 
    actmenu.get(0).click(); 
    actmenu.get(1).click(); 

所以,我能夠打印鏈接文本使用for循環,但我無法點擊元素,拋出以下錯誤:

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.31 seconds 

有沒有在解決這個錯誤的任何幫助?

+0

你在UI中檢查了這個元素嗎?它是可見的還是隱藏的? –

+0

你可能會看看[這篇文章](https://stackoverflow.com/questions/6101461/how-to-force-selenium-webdriver-to-click-on-element-which-is-not-currently- visib)對於可見元素的相同問題 –

+0

它是UI中的一個可見元素,我可以使用for循環獲取下拉菜單中所有鏈接的文本。我無法點擊它 – Sunny

回答

2

方法.getText()從HTML源返回值。也許你正在嘗試點擊的元素尚未呈現?試圖等待元件

WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOf(element);

上面的代碼將等待10秒鐘,直至元件是可見的。

相關問題