How to capture screenshot of a WebElement within a webpage but not the entire screen or page through Selenium(如何通过 Selenium 在网页中捕获 WebElement 的屏幕截图,而不是整个屏幕或页面)
问题描述
我必须捕获特定网站图像的屏幕截图.也许这是整个屏幕 20% 的折扣,我使用了下面的代码,它正在捕获整个屏幕.这对我解决问题没有帮助.
根据您的代码试验 getScreenshotAs()
方法将截取整个页面.
要在特定网页中捕获 WebElement 的屏幕截图,您可以使用导入 ashot-1.4 的 AShot()
方法.4.jar 同时使用 Selenium Java Client v3.14.0、ChromeDriver v2.41、Chrome v 68.0.p><块引用>
注意:来自 ashot-1.4.4.jar 的 AShot()
方法仅适用于
I have to capture a screenshot of an image of a particular website. Maybe this is 20% off entire screen, I have used below code, it is capturing the entire screen. Which is not helping me to solve the problem.
As per your code trials getScreenshotAs()
method will take the screenshot of the entire page.
To capture the screenshot of an WebElement within a particular webpage you can use the AShot()
method importing ashot-1.4.4.jar while working with Selenium Java Client v3.14.0, ChromeDriver v2.41, Chrome v 68.0.
Note:
AShot()
method from ashot-1.4.4.jar works only with jQuery enabled Web Applications.
So as the website http://www.google.com/
is not jQuery enabled AShot()
method from ashot-1.4.4.jar won't be able to take the required screenshot.
As an example we will take a snapshot from the website https://jquery.com/
.
Code Block:
Screenshot:
Reference
You can find a detailed discussion in How to take screenshot with Selenium WebDriver
这篇关于如何通过 Selenium 在网页中捕获 WebElement 的屏幕截图,而不是整个屏幕或页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!