【问题标题】:Should all elements return true on isDisplayed() after a selenium Webdriver get()?在 selenium Webdriver get() 之后,所有元素都应该在 isDisplayed() 上返回 true 吗?
【发布时间】:2012-09-21 14:31:06
【问题描述】:

WebDriver get() 和 isDisplayed() 方法没有按我的预期工作。

至于doc

这是使用 HTTP GET 操作完成的,该方法将阻塞 直到加载完成

正如Wait for page load in Selenium 之类的其他问题所述,get 方法应该等待页面加载。

但是在运行 get() 之后,来自 RenderedWebElement 的 isDisplayed() 方法并不总是在某些元素上返回 true。

可能的原因是什么?

我想详细说明在 webdrivers 上下文中加载和显示之间的区别。

【问题讨论】:

    标签: html asynchronous selenium webdriver rendering


    【解决方案1】:

    在最新的 UI 框架/API 中,您可以隐藏页面上的元素。

    例如。考虑一个有 5 个元素的页面。当页面加载时,页面上只会显示 3 个元素,另外 2 个将被隐藏,并且在执行某些操作时,其他 2 个元素将被显示。

    您可以在以下链接的演示部分查看示例:

    显示元素链接:http://api.jquery.com/show/

    隐藏元素链接:http://api.jquery.com/hide/

    当您使用 webDriver get() 方法时,webdriver 将等待页面加载,即等待页面的所有 html 内容加载到浏览器上。这并不意味着所有元素都是可见的。

    当你使用 isDisplayed() 时,webdriver 会检查该元素是否显示在页面上。如果您知道在运行测试用例时该元素可能隐藏在页面上,那么这是验证该元素是否显示的好方法。否则您的测试脚本将失败并显示错误“未显示元素以执行操作”

    希望这会有所帮助。

    【讨论】:

    • 所以你的意思是,如果一个元素需要在某个测试中被点击,它还不足以完全由 WebDriver 加载——它还必须由 WebDriver 显示,并且有一件事可以不是暗示对方吧?如果是这种情况,假设我在原始 html 文件中的元素是 not hidden 并且它的 display 属性是 not equal to "none" 。这是否意味着,如果元素被加载,它必然会显示在 WebDriver 上?
    • 隐藏/显示的元素不是 WebDriver 属性,实际上是 Html 属性,取决于开发 Web 应用程序的开发人员希望如何/何时显示它们。
    • 被隐藏/显示的元素不是 WebDriver 属性,实际上是 Html 属性,取决于开发 Web 应用程序的开发人员希望如何/何时显示它们。。 Webdriver 只是帮助您识别元素是否显示在页面上。 。关于您的问题 - 如果元素是基于“显示”属性显示的,那么它并不意味着它已加载到页面上。 考虑 Gmail——当您访问该页面时会加载该页面,但其中的内容可能一段时间后或滚动时加载。
    • 我知道,但我的问题没有得到解答。问题是“回答对或错:对于应该在 html 页面上显示的 html 元素(开发人员以这种方式编码),页面已完全加载的事实意味着 isDisplayed() 应该始终为它返回 true ?” - 或者更直接 -"
      Hi
      - 当 WebDriver 的 get() 从加载此页面返回时,isDisplayed() on 'el' 应该总是返回 true 吗?”。这个问题很简单。我问这个问题是因为我期待一个“是”,但我显然从我的构建中得到一个“并不总是”。
    • this blob entry 中所写,有一种解决方法可以点击隐藏元素。此外,存在有时可能比显示更重要。这记录在this blog entry
    【解决方案2】:

    isDisplayed() 在我看来不是很好的方法。从等待中获得一些想法:显式和隐式等待机制:

    Explicit wait
    WebDriverWait.until(condition-that-finds-the-element)
    
    Implicit wait
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    
    Explicit Waits:
    
    WebDriver driver = new FirefoxDriver();
    driver.get("http://somedomain/url_that_delays_loading");
    WebElement myDynamicElement = (new WebDriverWait(driver, 10))
      .until(new ExpectedCondition<WebElement>(){
        @Override
        public WebElement apply(WebDriver d) {
            return d.findElement(By.id("myDynamicElement"));
        }});
    
    Implicit Waits:
    
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://somedomain/url_that_delays_loading");
    WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
    
    The example what you have given both do exact same thing.. 
    In Explicit wait, WebDriver evaluates the condition every 500 
    milliseconds by default ..if it is true, it comes out of loop 
    
    Where as in ImplicitWait WebDriver polls the DOM every 500 
    milliseconds to see if element is present.. 
    
    Difference is 
    1. Obvious - Implicit wait time is applied to all elements in your 
    script but Explicit only for particular element 
    2. In Explicit you can configure, how frequently (instead of 500 
    millisecond) you want to check condition. 
    3. In Explicit you can also configure to ignore other exceptions than 
    "NoSuchElement" till timeout.. 
    

    您可以获取更多信息here

    我还使用流畅的等待机制来等待元素在页面上呈现。实际上,您将 css 选择器或 xpath 传递给函数并简单地获取 web 元素。

    public WebElement fluentWait(final By locator){
            Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(30, TimeUnit.SECONDS)
                    .pollingEvery(5, TimeUnit.SECONDS)
                    .ignoring(NoSuchElementException.class);
    
            WebElement foo = wait.until(
    new Function<WebDriver, WebElement>() {
                public WebElement apply(WebDriver driver) {
                            return driver.findElement(locator);
                    }
                    }
    );
                               return  foo;              }     ;
    

    流利等待description

    希望现在清楚)

    【讨论】:

    • 我想详细说明在 webdrivers 上下文中加载和显示之间的区别 - 我将把它添加到问题中
    猜你喜欢
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多