1

Here's snippet of my code

while (!$driver.find_element(:xpath,element2).displayed?)
    $driver.find_element(:xpath,element1).click()
end

for some reason element.displayed? throws this exception:

An element could not be located on the page using the given search parameters. (RuntimeError)

I expected the code to return false when the element was not found but instead I got the exception.
Is this working as intended or am I using it incorrectly?

0

4 Answers 4

6

$driver.find_element(:xpath,element2)

Above, you are trying to find an element.

.displayed?

Now you are trying to check the state in a element.

The exception is thrown by the first computation; the displayed? is never executed.

Given that, in order to avoid code duplication and different behaviors in different parts of the suite, I'd recommend to you to create an Element class, which will wrap selenium calls. This class would provide a cleaner interface and, inside each method, you can add logic to deal with selenium behavior that you don't appreciate.

For instance, you can implement a isVisible? method which, beside use Selenium to check the visibility, you can check a html class or attribute as hidden.

2
  • 1
    yep, pretty much figured it out when i debug this. now i use this approach $driver.find_elements(:id, element).size == 0 || !$driver.find_elements(:id, element).displayed? but thanks for your answer and i can't believe i never thought to create a class for the element to avoid this things. Thanks! Commented Apr 12, 2017 at 4:33
  • Beware that this approach that you mentioned has a poor performace. You could call the .find_elements only once and make the two validations in a single object (resulted from that call). If you don't know, I'd recommend to study the Page Object design pattern; it is used frequently in GUI automation (What I described is part of this DP). Commented Apr 12, 2017 at 14:07
3

You are checking that an element that doesn't exist is displayed. Elements must exist if they are displayed so you are failing before you get to the .display?

For the findElement part, you need to add a rescue and from there you could rescue the error and provide a useful message:

rescue Selenium::WebDriver::Error::NoSuchElementError
    raise Selenium::WebDriver::Error::NoSuchElementError.new("No Such Element: #{elementLocator}")

It would be better to separate out the methods of finding the elements and checking if they are displayed, that way you avoid duplicate code and can better handle the exceptions.

For more info on handling exceptions in Ruby: http://rubylearning.com/satishtalim/ruby_exceptions.html

0

The error you have reported seems to indicate that the element is not found. Therefore your issue is not on the isdisplayed? part, but more on the find_element(:xpath,element2) part

When your code executes, the element on which :xpath,element2 points, is not present (in the html/xml). Possible fix:

  • wrong xpath --> test it if you can, and if it is not Okay, and get an accurate xpath.
  • the element is not present anymore / or not yet present. you may be facing an asynchronous issue (also called race condition). find something in the code you are testing, that will allow you have the good synchrisation
  • There are probably multiple other posibilities... I gave you the two most important ones that came to my mind.

I got the same issue today (and during the past few days), although the fact that I am programming in java, it looks to me like you are having the same issue. I guess it is a classic one, when using Selenium or Appium: getting the right locator, and avoiding asynchronous issue.

1
  • This doesn't really answer the question: the user wants to know why they can't use a simple boolean check for displayed when the element isn't present. You've hinted at the answer (that the element not found exception is going to prevent the boolean operation) but you need to make this much clearer - especially since the other two answers are clear.
    – Kate Paulk
    Commented Apr 11, 2017 at 17:37
0

This is a common issue in using

find element

vs

find elements

As detailed at http://toolsqa.com/selenium-webdriver/findelement-and-findelements-command/

The difference between FindElement & FindElements Commands

The difference between findElement() and findElements() method is the first returns a WebElement object otherwise it throws an exception and the latter returns a List of WebElements or an empty list if no DOM elements match the query.

findElement()

On Zero Match : throws NoSuchElementException
On One Match : returns WebElement
On One+ Match : returns the first appearance in DOM 

findElements()

On Zero Match : return an empty list
On One Match : returns list of one WebElement only
On One+ Match : returns list with all matching instance

It's not a boolean (you can wrap to get that) but I think this addresses your underlying issues, i.e. getting the exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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