0

My first question here. I hope this question is not already answered. I previously searched if it exists, sorry if I'm wrong.

My question is the next. I have this webelement in a PageObject class for automated tests:

//Customer filter
@FindBy(id = "customer_filter")
private WebElement customerFilter;

Later I try to check if it's present or not, like this:

Boolean test = customerFilter.isDisplayed();

But it doesn't work, it says the webelement is not present when actually is not present, and the test ends. I've also tried with isEnabled() and isSelected(). I have to use instead the next code so everything works:

Boolean isPresent = driver.findElements(By.id("customer_filter")).size() > 0;
    if(isPresent){

Is there a way to use webelement directly so I don't have to continuosly use the id locator?

Thanks in advance!!!

Edit: Looking for a little more information, I found this thread about the same problem, but it wasn't resolved: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1880

Rune
  • 1
  • 1
  • Which version are you using.. if your using older version try to update it and also giving some wait time for element to be present on page is ideal solution – Ranjith's Jun 09 '16 at 14:45
  • I'm using Selenium 2.52. I used wait time and it doesn't work. – Rune Jun 09 '16 at 15:00
  • have you tried using latest version.. – Ranjith's Jun 09 '16 at 15:03
  • "it says the webelement is not present when actually is not present" - is it a typo or you are saying it behaves correctly? Try to wait for it instead of find, using explicit wait: http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-waits – timbre timbre Jun 09 '16 at 15:19
  • Sorry, I want to say that when the element is not present, the next exception appears: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"customer_filter"} – Rune Jun 10 '16 at 09:56

2 Answers2

1

When you use the "@FindBy" annotation, it returns a WebElement Proxy. The WebElementProxy is a wrapper around webelement, and has a "UnderlyingWebElement" property, which is what you're looking for.

https://github.com/barancev/webdriver-samples/blob/master/src/ru/st/selenium/WebElementProxy.java#L141

How you can leverage this, is you can do some creative typecasting to access some of these methods that are not in the IWebElement interface.

if( ((WebElementProxy)customerFilter).getWrappedElement() != null) { 
  //do something
}
David Lai
  • 814
  • 7
  • 13
  • Could you explain a bit more about your suggestion? I'm new with Selenium and I don't understand some concepts. – Rune Jun 10 '16 at 09:59
  • There's 2 concepts to understand. First, the @FindBy actually returns a proxy object, for the most part it works exactly like a webelement, but in the case you want to use it for, the underlying webelement isn't available yet. So the 2nd concept is typecasting. By typecasting into a WebElementProxy, you can then access some of the properties that are part of WebElementProxy that'll let you check if the underlying webelement is available. – David Lai Jun 20 '16 at 18:40
  • When I insert your code in Eclipse, it displays this error twice: "WebElementProxy cannot be resolved to a type". And there's no suggestions in the IDE to fix it. By the way, thanks for your clarification. – Rune Jun 22 '16 at 07:48
  • Turns out the WebElementProxy is in the C#/.Net implementation, the Java version doesn't have that. – David Lai Jun 30 '16 at 16:04
0

Please refer to this post:post

Also have in mind that when you're checking if .size()>0, this has nothing to do with either visible or presented element. This collection contains all element in the DOM that are matching the criteria.

My advise is to create a method that you call each time with parameter the element that you're looking for.

Community
  • 1
  • 1