WebElement.isElementUsable()

274 views
Skip to first unread message

bitkidoku

unread,
Dec 18, 2009, 7:21:46 AM12/18/09
to webdriver
Is there such a functionality like WebElement.isElementUsable() yet?

Right now I am trying to click() on an element, if I get an
ElementNotVisibleException I understand it is not usable.

Are there any other alternatives?

Thank you for your answers.

Jason

unread,
Dec 18, 2009, 12:46:33 PM12/18/09
to webdriver
WebDriver does not let you click on an element if the user would not
be able to click on it. This means two conditions must hold: 1) the
element has width and height, and 2) the element is not hidden by CSS

There are several methods you can call to query the state of your
element[1]:

element.getSize() - // Returns the size of the element (condition 1)
element.isDisplayed(); // Returns whether the element is displayed on
the page (condition 2)

-- Jason

[1] You can find the full javadoc for WebDriver here:
http://selenium.googlecode.com/svn/webdriver/javadoc/index.html

Miki

unread,
Dec 18, 2009, 1:16:29 PM12/18/09
to webdriver
Hello,

> Is there such a functionality like WebElement.isElementUsable() yet?

I don't think it's currently exposed by the API, which language are
you using?

--
Miki

bitkidoku

unread,
Dec 19, 2009, 7:43:27 AM12/19/09
to webdriver
I am using Java

bitkidoku

unread,
Dec 19, 2009, 7:45:11 AM12/19/09
to webdriver
afaik, isDisplayed() is a method of RenderedWebElement not WebElement,
but it is possible to get ElementNotVisible exception even in
HtmlUnitDriver. I am looking for a generic solution.

> > Thank you for your answers.- Alıntıyı gizle -
>
> - Alıntıyı göster -

Simon Stewart

unread,
Dec 20, 2009, 10:47:31 AM12/20/09
to webd...@googlegroups.com
If "isDisplayed" returns false, then you're not going to be able to
click. A call such as "click" can cause at least two exceptions, the
"ElementNotVisibleException" (checked for with "isDisplayed") and the
"StaleElementException", which will always be thrown if the element
being referenced no longer exists (because you've refreshed the page,
for example). There's a number of ways to get around this problem, but
the most expressive way is probably to make use of the Wait interface.

First, create a function that allows you to figure out if the element
is visible yet (I'm writing this into my email client, so it may not
compile: you get the idea)

public ExpectedCondition<WebElement>
visibilityOfElementLocated(final By by) {
return new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver driver) {
WebElement element = driver.findElement(by);
if (!(element instanceof RenderedWebElement))
return element;

return ((RenderedWebElement) element).isDisplayed() ?
element : null;
}
};
};

Note that the function returns a WebElement. This is because the
"until" method of "Wait" returns whatever the function does. This
means that we can avoid an extra element lookup, and our tests become
a little bit more readable. We can then use it, like so:

Wait<WebDriver> wait = new WebDriverWait(driver);
WebElement element = wait.
until(visibilityOfElementLocated(By.id("foo:"));

It should also be possible to use the PageFactory and a (unwritten)
ElementLocatorFactory that waits until an element is visible before
calling (certain?) methods on an element.

Which is a really long way of saying that I have no plans to add an
"isUsable" method to an element because there are already ways of
handling what you want. I hope that the reasoning makes sense and that
this helps you figure out a nice way of doing what you need.

Regards,

Simon

2009/12/19 bitkidoku <b.e...@gmail.com>:

> --
>
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>
>
>

bitkidoku

unread,
Dec 21, 2009, 7:26:15 AM12/21/09
to webdriver
Thank you very much for your informative answer. I was already aware
of Wait, but your example made it even clearer.

I see a cast to RenderedWebElement in apply(), this means this Wait
function will only be available for drivers other than HtmlUnit. What
I am looking for is a way to achieve the same thing regardless of the
driver.

As I explained before, the best way I found so far is to click() on an
element, so I know if it is usable or not (if click throws
ElementNotVisible I know that it is not usable), and this approach
works with all drivers. But this solution looks dirty, and does not
feel right; that is why I asked for a better way.

Frankly, if all drivers can throw ElementNotVisibleException, an
element should tell if element itself is visible (or usable)
regardless of driver.

I am sorry for my English, since it is not my native language, I may
not have made myself clear.

> 2009/12/19 bitkidoku <b.ev...@gmail.com>:


>
>
>
> > afaik, isDisplayed() is a method of RenderedWebElement not WebElement,
> > but it is possible to get ElementNotVisible exception even in
> > HtmlUnitDriver. I am looking for a generic solution.
>

> > On 18 Aralýk, 19:46, Jason <jmle...@gmail.com> wrote:
> >> WebDriver does not let you click on an element if the user would not
> >> be able to click on it.  This means two conditions must hold: 1) the
> >> element has width and height, and 2) the element is not hidden by CSS
>
> >> There are several methods you can call to query the state of your
> >> element[1]:
>
> >> element.getSize() - // Returns the size of the element (condition 1)
> >> element.isDisplayed();  // Returns whether the element is displayed on
> >> the page (condition 2)
>
> >> -- Jason
>
> >> [1] You can find the full javadoc for WebDriver here:http://selenium.googlecode.com/svn/webdriver/javadoc/index.html
>
> >> On Dec 18, 4:21 am, bitkidoku <b.ev...@gmail.com> wrote:
>
> >> > Is there such a functionality like WebElement.isElementUsable() yet?
>
> >> > Right now I am trying to click() on an element, if I get an
> >> > ElementNotVisibleException I understand it is not usable.
>
> >> > Are there any other alternatives?
>

> >> > Thank you for your answers.- Alýntýyý gizle -
>
> >> - Alýntýyý göster -

Simon Stewart

unread,
Dec 24, 2009, 1:50:34 PM12/24/09
to webd...@googlegroups.com
If you look at the implementation of the wait, we do a check to see if
the element implements the "RenderedWebElement" before doing the cast,
meaning that it can be used even with the HtmlUnitDriver without JS
enabled, and it should do what you expect.

Regards,

Simon

bitkidoku

unread,
Dec 25, 2009, 7:27:51 AM12/25/09
to webdriver
Oh! I understood the RenderedWebElement all wrong then, I always
thought HtmlUnit driver does not produce RenderedWebElements. Thank
you very much.
Reply all
Reply to author
Forward
0 new messages