无法从下拉列表中选择导致ElementNotVisibleException的选项 import java.util.List; 导入java.util.concurrent.TimeUnit; 导入org.openqa.selenium.By; 导入org.openqa.selenium.WebDriver; 导入org.openqa.selenium.WebElement; 导入org.openqa.selenium.chrome.ChromeDriver; 导入org.openqa.selenium.firefox.FirefoxDriver; 导入org.openqa.selenium.support.ui.Select; 公共类DellDropdown{ 公共静态void main(字符串[]args){ WebDriver=newfirefoxdriver(); driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); 驱动程序。获取(“http://www.dell.com/"); WebElement dropdown=driver.findElement(By.xpath(//select[@class='para_small']); 选择=新选择(下拉菜单); select.selectByIndex(5); List options=select.getOptions(); 对于(int i=0;i

无法从下拉列表中选择导致ElementNotVisibleException的选项 import java.util.List; 导入java.util.concurrent.TimeUnit; 导入org.openqa.selenium.By; 导入org.openqa.selenium.WebDriver; 导入org.openqa.selenium.WebElement; 导入org.openqa.selenium.chrome.ChromeDriver; 导入org.openqa.selenium.firefox.FirefoxDriver; 导入org.openqa.selenium.support.ui.Select; 公共类DellDropdown{ 公共静态void main(字符串[]args){ WebDriver=newfirefoxdriver(); driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS); 驱动程序。获取(“http://www.dell.com/"); WebElement dropdown=driver.findElement(By.xpath(//select[@class='para_small']); 选择=新选择(下拉菜单); select.selectByIndex(5); List options=select.getOptions(); 对于(int i=0;i,java,selenium-webdriver,Java,Selenium Webdriver,您可能需要等待下拉列表实际可见,然后再选择选项: 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 o

您可能需要等待下拉列表实际可见,然后再选择选项:

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

谢谢@aquaraga,下面的代码在执行时给了我StaleElementReferenceException,我该如何解决它呢?对于(int i=0;我的意思是你仍然会得到异常。如果是,你能告诉我在哪里吗?只有在满足等待条件后才定义“dropdown”。也就是说,“WebElement dropdown=driver.findElement”(By.xpath(//select[@class='para_small']');”应该在wait语句之后执行。否则,web驱动程序将获得“dropdown”的过时引用,这就解释了为什么会出现异常。
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@class='para_small']")))