硒 VBA : driver.captureEntirePageScreenshot.Copy

Selenium VBA : driver.captureEntirePageScreenshot.Copy

论坛新手,Selenium 新手 VBA。为我的无知提前道歉。 但是,我已经搜索了任何 VBA 版本,它可以在不使用插件或不使用 Java.

的情况下捕获整页元素

我想尽可能简单地捕获整页屏幕截图,然后将其从剪贴板粘贴到 excel 内的工作表中。

我目前的代码我知道它可以改进,其中一些可能是不必要的,但我不知道为什么它在 bot.TakeScreenshot.Copy 确实有效但只捕获可见屏幕时不起作用。

Public Sub CommandButton1_Click()

Dim bot As New WebDriver, rng As Range
Set rng = Range(Worksheets("sheet1").Range("A2"), 
Worksheets("sheet1").Range("A2").End(xlDown))

'this loops through each href in cells navigating to the page unless the cell has end in it

For Each cell In rng
TextBox2.Text = ActiveCell.Text
TextBox3.Text = ActiveCell(1, 2).Text
If ActiveCell.Text = "End" Then
bot.Quit
Else
bot.Start "chrome", TextBox2.Text
bot.Window.Maximize
bot.Get "/"

'this takes a screenshot of each url in the loop and copys it

bot.captureEntirePageScreenshot.Copy

'bot.TakeScreenshot.Copy

'this creates a new sheet names it according the relavent test related to the url and pastes it in that newlt created sheet

ActiveWorkbook.Sheets.Add.Name = ActiveCell(1, 2).Text
ActiveSheet.PasteSpecial

'this goes back to the original sheet to continue with loop

Worksheets("sheet1").Select
ActiveCell.Offset(1, 0).Select

bot.Quit
End If
Next

End Sub

使用无头浏览器

这取自selenium自带的示例代码

在win7机器上... C:\Users\xxxx\AppData\Local\SeleniumBasic\Examples\VBScript

Option Explicit

Sub demoPhantomJSDriver()

    Dim aaa As Selenium.PhantomJSDriver
    Set aaa = New Selenium.PhantomJSDriver
    aaa.Get "https://whosebug.com/questions/tagged/vba?sort=newest&pageSize=50"
    aaa.Window.Maximize

    Dim bbb As Object
    Set bbb = aaa.TakeScreenshot
    bbb.ToExcel Range("h3")
    Set bbb = Nothing

End Sub