【问题标题】:cannot select from a dropdpwn menu on firefox无法从 Firefox 的下拉菜单中选择
【发布时间】:2017-01-16 23:35:02
【问题描述】:

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

 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 对象。

【问题讨论】:

  • 您是否已验证 XPath 在 FF 上有效?没有相关的 HTML 就很难提供帮助。
  • 那么您是否等到它使用WebDriverWait 变得可见??
  • 试试 CSS 选择器而不是 xpath。 Coz xpath 可能在所有浏览器中都不相同。
  • @Jeffs 是的,FF 和 Chrome 的 xpath 是相同的,我们也使用了 wait.unit(...visibilityOfElementLocatedBy(..))

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    使用fluent wait to wait元素,chrome更快:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-16
      • 2019-07-30
      • 2021-06-28
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      相关资源
      最近更新 更多