Screenshot Comparison for Selenium Tests

Liviu Lupei
Dec 5, 2017 · 2 min read

You might have seen the Screenshot Comparison feature from Endtest:

Today, I’m going to show you how to code one for yourself in Python:

But you might have a lot of different elements on your page, you might even have some ads. Your entire page probably won’t look the same if you go to it several times.

That’s why it’s important to have the option to do a Screenshot Comparison between elements.

Selenium doesn’t give you the option to take a screenshot only of a certain element, that’s why we’re going to use a clever workaround.

We’re going to find the element, get the coordinates from the element, take a screenshot of the entire page and crop that screenshot.

We just need to integrate this piece of code into our compareScreenshots() method.

And we’re done.

In case you want to see how a proper Screenshot Comparison works, just head on down to Endtest.

Liviu Lupei

Written by

Senior Software Engineer