An example how to take element screenshot using Selenium 4 devtools API.
Take element screenshot
First step is to get DevTools
and create session:
After that we can use chromeDevTools
for taking screenshots. In order to do this we need to utilize
Page.captureScreenshot
method:
The screenshot will be saved with ‘github_screenshot.png’ name to project’s root folder and attached to Allure test report.
After all required actions and verifications let’s clean up and delete our screenshot:
Voilà. In the same manner it is possible to take screenshots of full page with the help of devtools in Selenium 4.
Full code example is here.