0

I'm using Selenium web driver and Python to create automation scripts for web application testing. I need to implement verification that will compare two strings of encoded png files as base64: saved basic image and current image on page of same web element. There is a method in Selenium that allow to get page screenshot as base64 object

driver.get_screenshot_as_base64()

But how to get base64 screen of not the whole page, but just of particular image element on page without downloading it?

P.S. Other ways of comparing two images are acceptable also:)

Andersson
  • 51,635
  • 17
  • 77
  • 129

2 Answers2

0

There is an answer to another question that explains how to take a screenshot of an element here. Once you have that, you should be able to do a pixel by pixel comparison of the two images. You can google and find code examples for that.

I don't see a lot of info on base64 images. It seems like it would be a really cool, easy way to compare two images since you'd just do a quick string compare but selenium doesn't seem to support taking a screenshot of an element in base64. You could probably do some work to take the screenshot, convert it and the reference image to base64, but that would likely be more work than just using a library or comparing two images that has been done a bunch of times before and is all over the web.

Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55
0

The following should work according to the docs, but does not, and there is an open issue for it here: https://github.com/SeleniumHQ/selenium/issues/912. In the meantime, I would suggest https://stackoverflow.com/a/15870708/1415130

Find you web page element however you want - see docs Locating Elements

login_form = driver.find_element_by_id('loginForm')

Then screen grab the element

screenshot = login_form.screenshot_as_base64()

To compare screenshots, I'm using Pillow.

Community
  • 1
  • 1
birdsarah
  • 1,165
  • 8
  • 20
  • This not works with `Firefox`. What browser you use? `Edge`? – Andersson Dec 08 '15 at 07:28
  • Apologies, you are correct, it's an open issue: https://github.com/SeleniumHQ/selenium/issues/912 – birdsarah Dec 10 '15 at 08:41
  • Also doesn't work on PhantomJS. For phantom throws `WebDriverException: Message: Invalid Command Method` and for Firefox `WebDriverException: Message: Unrecognized command`. A workaround is using Javascript to create canvas from images, then save it base64 later. – m3nda May 29 '16 at 15:17
  • For me it was get_screenshot_as_base64() – James Baird Feb 01 '23 at 20:49