无法从firefox上的dropdpwn菜单中选择

时间:2016-09-08 22:20:26

标签: java selenium selenium-webdriver selenium-chromedriver selenium-firefoxdriver

我正在使用 Java Selenium 来编写测试。我有一个下拉菜单,我需要从中选择一些东西。这是我的代码:

 Select s= new Select(driver.findElement(By.xpath("blabla")));
 s.selectByVisibleText("theName");

适用于 Chrome ,但 Firefox 47我收到此错误:

org.openqa.selenium.ElementNotVisibleException: 
Element is not currently visible and so may not be interacted with

我知道如何通过其他方式处理从下拉菜单中选择,但我需要使用Select对象。

2 个答案:

答案 0 :(得分:0)

使用流畅的等待等待元素,铬更快:

public static void waitUntilElementIsVisible(WebElement element, WebDriver driver) 
{        
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
    wait.pollingEvery(250,  TimeUnit.MILLISECONDS);
    wait.withTimeout(2, TimeUnit.MINUTES);
    wait.ignoring(ElementNotVisibleException.class); //make sure that this exception is ignored
    Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>()
            {
                public WebElement apply(WebDriver driver) {
                    System.out.println("Checking for the element!!");                       
                    if(element.isDisplayed() != true)
                    {
                        System.out.println("Target element is not visible");
                    }
                    return element;
                }
            };

    wait.until(function);
}

然后你可以打电话给它:

WebElement el = driver.findElement(By.xpath("blabla"));
waitUntilElementIsVisible(el, driver);

答案 1 :(得分:0)

您可以使用WebDriverWait类来等待元素的可见性,如下所示:

WebDriverWait wait = new WebDriverWait(driver, customTime);
WebDriver driver = new FirefoxDriver();

Select s = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("blabla")));
s.selectByVisibleText("theName");