Python+Selenium: AssertionError: Lists differ: [] != ["'This[99 chars] that\\n you give the Sto[183 chars]it."]

Python+Selenium: AssertionError: Lists differ: [] != ["'This[99 chars] that\\n you give the Sto[183 chars]it."]

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Python+Selenium: AssertionError: Lists differ: [] != ["'This[99 chars] that\\n you give the Sto[183 chars]it."]

Solving Assertion Errors in Selenium Python Tests: A Comprehensive Guide

Hello developers! Today we’re going to tackle a common issue faced by many of us when writing automated tests using Python and Selenium - the dreaded AssertionError that fails despite our text verification appearing correct.

Understanding the Issue

Our friend here is working on a Magento Cloud project, using Chrome with WebDriver 78x. They have a block of text on their page which they’re trying to verify through an assertion in their test case. However, every time the test runs, it fails with this mysterious AssertionError.

The error message suggests that there’s a discrepancy between what we expected (an empty list) and what we actually got - a list containing text from the webpage.

self.assertEqual([], self.verificationErrors)
AssertionError: Lists differ: [] != ["'This[99 chars] that\\n you give the Sto[183 chars]it."]

Second list contains 1 additional elements.
First extra element 0:
"'This[99 chars] that\\n you give the Store while using this we[441 chars]ges.' != 'This[99 chars] that you give the Store while using this webs[431 chars]ges.'\nDiff is 1188 characters long. Set self.maxDiff to None to see it."

The source code provided shows a simple test case attempting to assert that the text on the page matches the expected string.

def test_untitled_test_case(self):
    driver = self.driver
    driver.get("https://URL-TO-PROJECT.com")
    self.assertEqual("This privacy policy sets out how this website (hereafter \"the Store\") uses and protects any information that\n you give the Store while using this website. The Store is committed to ensuring that your privacy is protected.\n Should we ask you to provide certain information by which you can be identified when using this website, then\n you can be assured that it will only be used in accordance with this privacy statement. The Store may change\n this policy from time to time by updating this page. You should check this page from time to time to ensure\n that you are happy with any changes.", driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Privacy and Cookie Policy'])[3]/following::p[1]").text)

Analyzing the Problem

The error message clearly indicates a difference between what’s expected ([]) and what was found on the page. However, the provided source code suggests that the assertion should pass because it’s checking for an empty list ([]).

Looking at the code snippet, there seems to be a mismatch in the assertEqual method - they’re comparing a string from the webpage with an empty list.

This leads us to believe that self.verificationErrors is not actually returning what we expect it to.

Possible Causes and Solutions

Issue with WebDriver Setup

One potential issue could be related to how the WebDriver is configured or executed. Ensure that you have the correct version (78x in this case) installed and properly configured with your Python environment.

You may want to double-check if there are any discrepancies between the versions of Selenium and Chrome being used, as compatibility issues can sometimes cause unexpected behaviors.

Text Encoding Issues

Another possibility is a problem with text encoding when retrieving elements from the webpage. Selenium might be interpreting certain characters differently than expected, leading to the discrepancy in the assertion.

To troubleshoot this:

  • Check if there are any special characters or non-ASCII characters in your expected string that could cause issues.
  • Try using different methods of retrieving the text (like get_attribute('textContent') instead of .text) to see if it affects the output.

Race Condition with Multiple Threads

If you’re running multiple tests concurrently, there could be a race condition where the test case is trying to interact with an element that hasn’t fully loaded yet or is being updated by another concurrent process.

To diagnose this:

  • Ensure you’re not running tests in parallel if they rely on shared state.
  • Use explicit waits (like WebDriverWait) instead of implicit waits to give elements enough time to load before interacting with them.

Best Practices for Text Verification

When writing test cases involving text verification, follow these best practices:

  1. Use WebDriverWait: Always use explicit waits (WebDriverWait) when interacting with dynamic content. This ensures that your tests wait until the element is ready before attempting to retrieve its text.

  2. Avoid Using XPath: While XPath can be powerful, it’s also more prone to issues like changing DOM structures breaking your test cases. Consider using CSS selectors or even direct element IDs if possible for better stability.

  3. Use AssertJ or Hamcrest for Better Assertions: Libraries like AssertJ and Hamcrest provide more robust assertion mechanisms with improved error messages when assertions fail.

  4. Clean Up Test Data: Ensure that any test data created during the course of a test is properly cleaned up afterward to avoid cross-test contamination or unexpected state changes.

  5. Document Your Assumptions: Clearly document what your tests are supposed to verify and under what conditions they should pass or fail. This helps in debugging when issues arise.

Conclusion

In summary, this AssertionError appears to be a result of comparing an empty list with the text content retrieved from the webpage. The issue likely lies with how self.verificationErrors is being populated or how the WebDriver is interacting with the element containing the text.

To resolve this:

  • Double-check your WebDriver setup and ensure compatibility.
  • Verify that there are no encoding issues when retrieving the text.
  • Examine if any race conditions could be causing the discrepancy.
  • Consider refactoring your test case to follow best practices for text verification, such as using explicit waits and cleaner assertions.

Remember, the key is to understand what each part of your test case is doing and isolate where the breakdown might occur. Happy debugging!

Don’t forget to explore our Selenium WebDriver guide for more in-depth coverage on working with Selenium in Python.

Keep coding!

Errors

Our network