我正在使用Selenium web driver
和Python
为Web应用程序测试创建自动化脚本。我需要实现验证,将两个编码的png
文件串作为base64
进行比较:保存基本图像和同一网页元素页面上的当前图像。 Selenium
中有一种方法可以将页面屏幕截图设为base64
对象
driver.get_screenshot_as_base64()
但是如何获得base64
屏幕而不是整个页面,而只是页面上的特定图像元素而不下载它?
P.S。比较两个图像的其他方式也是可以接受的:)
答案 0 :(得分: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.
答案 1 :(得分:0)
以下内容应根据文档进行操作,但不是,并且此处有一个未解决的问题:https://github.com/SeleniumHQ/selenium/issues/912。与此同时,我建议https://stackoverflow.com/a/15870708/1415130
根据需要找到您的网页元素 - 请参阅文档Locating Elements
login_form = driver.find_element_by_id('loginForm')
screenshot = login_form.screenshot_as_base64()
为了比较屏幕截图,我使用Pillow。