0

I am repeatedly getting a very annoying problem when automating my test cases using Selenium/C# in Visual Studio.

This test is designed around validating search criteria text. In order for me to test each criteria, I have to:

  1. Navigate to the the app's homepage
  2. Click on the search app
  3. On the search page, enter valid criteria
  4. On the search results page, assert that the search criteria appears in text as what was searched for.
  5. Repeat steps 1-3 with different search criteria.
  6. Verify all search criterias on the results page appear as they should.

When I get to step 5 (navigating back to the homepage), I get the following exception: "threw exception: OpenQA.Selenium.StaleElementReferenceException: Element is no longer valid"

I had gotten this before and got around it by adding a "wait.Until(ExpectedConditions.ElementIsVisible" command- not entirely sure why this worked.

However, the above solution is becoming very tedious as it requires me to enter an instance for EACH element that I call to, which will end up being x30 more.

Any help will be greatly appreciated.

  • please include the pertinent section of your test script and the troublesome page objects – Doug Clark Jul 26 '18 at 18:47
0

This is a potential risk when you try performing actions in element in a loop. I would suggest put a break statement when you find the element as this is the nature of looping to fail once before exit.

You can do something like:

public void select_customer_short_view(String _name) {
            for (WebElement we : get_list_cust_components_names_webelement()) {
                if (we.getText().equals(_name)){                                    // this will throw StaleElementReferenceException if break is not put
                    we.click();
                    break;
                }
            }
        }
  • Thank you for replying. I will try your solution soon. Another workaround suggested by a coworker was to convert the element being asserted into a string with the x-path and then later call that string on each instance. – hungryhippos Aug 14 '18 at 21:11
  • Will that be readable and debug-able ? – Kshetra Mohan Prusty Aug 15 '18 at 17:04
  • If I am understanding you correctly, yes. I have been able to debug stuff with breakpoints and whatnot. – hungryhippos Aug 24 '18 at 19:06

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.