2

In my Java WebDriver test I use method findElements() to check if an element is present on the page. I use the Page Object Pattern, so the element I want to check is declared in SomePage.class. In my test I would like to just refer to declared element, but method findElements() requires me to use By type and look for the element again.

Is there any way to declare WebElement once and then refer to it in findElements() method?

  • The situation also happens with other methods which require By argument but not support WebElement argument. Would be nice to find general solution to problems like that. – kotoj Mar 25 '16 at 9:53
3

If you already found the element, why are you looking it up again? Maybe you should not use findElements() again, because that will try to find the elements based on a By. It cannot find based on a found element reference.

Maybe just use a element.IsDisplayed() instead to check if the element is still available for interaction.

  • element.isDisplayed actually works. But I have no method element.exist, do you use Java? – kotoj Mar 25 '16 at 10:11
  • Got the exists from this answers: stackoverflow.com/a/18061480/1088584 Maybe it does not work for Java, will remove it from my answer. – Niels van Reijmersdal Mar 25 '16 at 10:29
  • The answer is about C# so maybe C# has exist() method. And what about situation when I want to check if declared element is NOT present. There are no method like element.isNotDisplayed. What should I do? – kotoj Mar 25 '16 at 10:31
  • 1
    To check an an element is not displayed you can use this function, but you need to catch the NosuchElementFound exception, when the exception occurs the element is not present. Check this answer for a code example: stackoverflow.com/a/27853677/1088584 – Niels van Reijmersdal Mar 25 '16 at 10:34
  • I knew that solution but I wondered if there are more pretty way or built-in method. Overall thank you for your usefull help. – kotoj Mar 25 '16 at 10:43
1

Would have preferred to comment as it's more of a tip then an answer but need more reputation for that.

A lot of these issues get solved with Selenide

You have lot of extra options to interact, or wait for, elements and as it's a skin over Selenium you can still use any Selenium command you'd like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.