Search code examples
excelvbaseleniumscreenshot

Selenium VBA : driver.captureEntirePageScreenshot.Copy


New to the forums, and new to Selenium VBA. Apologies in advance for my ignorance. However I have searched for any VBA version of this that captures the full page element without plug-ins or use of Java.

I want to keep this as simple as possible to capture a full page screenshot then paste that from clipboard into sheets within excel.

Code I have so far I understand it can be improved and some of it may be unnecessary but I can't tell why it won't work when bot.TakeScreenshot.Copy does work but only captures the visible screen.

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

Solution

  • use the headless browser

    this is taken from the example code that comes with selenium

    on win7 machine ... 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://stackoverflow.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