1

WebDriver get() and isDisplayed() methods are not working as I expected.

As for the doc:

This is done using an HTTP GET operation, and the method will block until the load is complete

As mentioned in other questions like this Wait for page load in Selenium , the get method should wait for the page to load.

But after running get(), isDisplayed() method from RenderedWebElement does not always return true on some elements.

What are the possible causes?

I'd like some elaboration on the difference between being loaded and being displayed in the context of webdrivers.

Community
  • 1
  • 1
galmeida
  • 221
  • 2
  • 10

2 Answers2

1

In the latest UI frameworks/APi you can hide an element on a page.

For ex. Consider a page having 5 elements. When page is loaded only 3 elements will be shown on the page, the other 2 will be hidden and on taking some action the other 2 elements will be shown.

An example you can check under the demo section in following links:

Show element link: http://api.jquery.com/show/

Hide element link: http://api.jquery.com/hide/

When you use webDriver get() method, webdriver will wait for page to load i.e. it waits for all the html content of the page to be loaded onto the browser. This does not means that all the elements are visible.

When you use isDisplayed() webdriver checks whether the said element is Displayed on the page or not. If you know that the element may be hidden on the page when running your test-cases its a good approach to verify whether the element is displayed or not. Else your test script fails with a error "Element not displayed to take an action"

Hope this helps.

Varun Menon
  • 390
  • 1
  • 6
  • So you're saying like, if an element needs to be clicked on some test its not enough that it was totally loaded by the WebDriver - it must be also displayed by the WebDriver, and one thing does not imply the other, right? If thats the case, suppose my element on the original html file is **not** hidden and its display property is **not equal to "none"** . Does it mean that, if the element is loaded it is necessarily displayed on the WebDriver? – galmeida Sep 25 '12 at 16:36
  • Element being Hidden/Displayed are not WebDriver properties and are actually Html properties and depends on how/when the developer who developed the web application wants to show them. – Varun Menon Sep 26 '12 at 07:27
  • Element being Hidden/Displayed are not WebDriver properties and are actually Html properties and depends on how/when the developer who developed the web application wants to show them.. Webdriver just helps you to identify whether the element is displayed on the page or not. . About you question - if the element is displayed based on the "display" property then it does not mean that its loaded on the page. Consider Gmail -- The page is loaded when you access it bu the content inside it may get loaded after some time or when your scroll. – Varun Menon Sep 26 '12 at 07:36
  • I know, but my question was not answered. The question was "Answer true or false: For an html element supposed to be displayed(the developer coded it this way) on a html page, the fact that that the page is completely loaded implies that isDisplayed() should always return true for it?" - or even more directly -"
    Hi
    - when WebDriver's get() returns from loading this page, and isDisplayed() on 'el' should always return true?". The question is very simple.I'm asking because I expected a 'yes', but I'm apparently getting a 'not always' from my build.
    – galmeida Sep 26 '12 at 15:22
  • Yes. It should return "true" in case such cases. If you have issues with the element then you can do wait for element to be displayed after driver.get() before taking an action on the element. – Varun Menon Sep 28 '12 at 10:22
  • 1
    There is a work around for clicking on hidden elements as written in [this blob entry](http://brantleytec.blogspot.com/2013/02/webdriver-click-on-hidden-links.html). Furthermore, exists is probably more important than displayed sometimes. That is documented in [this blog entry](http://brantleytec.blogspot.com/2013/08/webdriver-iwebelementexists.html) – Brantley Blanchard Aug 15 '13 at 17:23
0

isDisplayed() seems to me not so good approach. get some ideas from wait: exlplicit and implicit wait mechanisms:

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.. 

you can get some more info here

Also I use fluent wait mechanism for waiting elements get rendered on the page. Actually you pass either css selector either xpath to the function and simply get web element.

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

fluent wait description

hope this comes clear now)

Community
  • 1
  • 1
eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
  • I'd like some elaboration on the difference between being loaded and being displayed in the context of webdrivers - I'll add that to the question – galmeida Sep 24 '12 at 18:29