SeleniumBasic的GetScreenshot方法用于对网页或一个元素进行截图,截图的结果可以保存为本地图片,Base64字符串,或者字节数组。
Property AsBase64EncodedString As String 只读
Property AsByteArray As Byte() 只读
Sub SaveAsFile(fileName As String, [format As ScreenshotImageFormat = ScreenshotImageFormat_Bmp])
WD.URL = "https://www.baidu.com"
Dim form As SeleniumBasic.IWebElement
Dim keyword As SeleniumBasic.IWebElement
Dim button As SeleniumBasic.IWebElement
Set form = WD.FindElementById("form")
Set keyword = form.FindElementById("kw")
Set button = form.FindElementById("su")
Dim pic As SeleniumBasic.Screenshot
Set pic = WD.GetScreenshot
pic.SaveAsFile "D:\Temp\baidu.png", SeleniumBasic.ScreenshotImageFormat_Png
Set pic = button.GetScreenshot
Debug.Print pic.AsBase64EncodedString
上述程序,打开百度首页,定位到文本框和按钮。
最后对网页截图,保存为png图片。按钮的截图输出为字符串。
D盘下产生如下清晰图片
立即窗口输出了一个字符串。