2014-04-10 32 views
0
import java.util.List; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class DellDropdown { 

    public static void main(String[] args) { 
     WebDriver driver=new FirefoxDriver(); 
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
     driver.get("http://www.dell.com/"); 
     WebElement dropdown=driver.findElement(By.xpath("//select[@class='para_small']")); 
     Select select=new Select(dropdown); 
     select.selectByIndex(5); 
     List<WebElement> options=select.getOptions(); 
     for(int i=0;i<options.size();i++) 
     { 
      options.get(i).getText(); 
     } 
    } 
} 

我收到ElementNotVisibleException在上線select.selectByIndex(5),有人可以幫助我在這方面的選項?無法從下拉導致ElementNotVisibleException

回答

1

您可能要等到下拉實際上是可見的,你的創業選擇的選項前:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@class='para_small']"))) 
+0

感謝@aquaraga,在執行下面的代碼給我StaleElementReferenceException,請問該如何解決呢? for(int i = 0; i user3296744

+0

你的意思是你仍然得到例外。如果是這樣,你能告訴我在哪裏? – aquaraga

+0

僅在滿足等待條件後才定義「下拉」。也就是'WebElement dropdown = driver.findElement(By.xpath(「// select [@ class ='para_small']」));'應該在等待聲明之後完成。否則,網絡驅動程序將獲得「下拉」的陳舊參考,這就解釋了您爲什麼會得到例外。 – aquaraga