Jump to content

Recommended Posts

Posted (edited)

The key for Chrome is to run WebDriver headless.  If anyone has suggestions to improve this or it is helpful to anyone, please feel free to comment.

#include "wd_core.au3"
#include "wd_helper.au3"
#include "wd_capabilities.au3"

Global $bHidden = True
Global $sCapabilities = SetupChrome($bHidden)
_WD_Startup()

Global $sSession = _WD_CreateSession($sCapabilities)
If @error = $_WD_ERROR_Success Then
    Global $sURL = 'https://autoitscript.com/forum'
    _WD_Navigate($sSession, $sURL)
    _WD_LoadWait($sSession)
    Global $sFile = 'screenshot.png'
    _FullscreenScreenshot($sSession, $sFile)
    ShellExecuteWait($sFile)
EndIf
_WD_DeleteSession($sSession)
_WD_Shutdown()

Func SetupChrome($bHeadless)
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\chrome.log"')
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'chrome')
    _WD_CapabilitiesAdd('w3c', True)
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    If $bHeadless Then _WD_CapabilitiesAdd('args', '--headless')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupChrome

Func SetupEdge($bHeadless)
    _WD_Option('Driver', 'msedgedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\msedge.log"')
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'msedge')
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    If $bHeadless Then _WD_CapabilitiesAdd('args', '--headless')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupEdge

Func SetupGecko($bHeadless)
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--log trace')
    _WD_Option('Port', 4444)
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'firefox')
    _WD_CapabilitiesAdd('browserName', 'firefox')
    _WD_CapabilitiesAdd('acceptInsecureCerts', True)
    If $bHeadless Then _WD_CapabilitiesAdd('args', '--headless')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupGecko

Func SetupOpera($bHeadless)
    _WD_Option('Driver', 'operadriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\opera.log"')
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'opera')
    _WD_CapabilitiesAdd('w3c', True)
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    _WD_CapabilitiesAdd('binary', _WD_GetBrowserPath("opera"))
    If $bHeadless Then _WD_CapabilitiesAdd('args', '--headless')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupOpera

Func _FullscreenScreenshot($sSession, $sFile)
    _WD_Window($sSession, 'rect', '{"height":1, "width":1, "x":0, "y":0}') ;This may need to be tweaked
    Local $iHeight = _WD_ExecuteScript($sSession, "return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.body.clientHeight, document.documentElement.clientHeight)", Default, Default, $_WD_JSON_Value)
    Local $iWidth = _WD_ExecuteScript($sSession, "return Math.max(document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth, document.body.clientWidth, document.documentElement.clientWidth)", Default, Default, $_WD_JSON_Value)
    _WD_Window($sSession, 'rect', '{"height":' & $iHeight & ', "width":' & $iWidth & ', "x":0, "y":0}')
    Local $sBodyElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByTagName, 'body')
    Local $dResponse = _WD_Screenshot($sSession, $sBodyElement, 2)
    Local $hFileOpen = FileOpen($sFile, $FO_BINARY + $FO_OVERWRITE)
    FileWrite($hFileOpen, $dResponse)
    FileClose($hFileOpen)
EndFunc  ;==>_FullscreenScreenshot

The _WD_Window command may need to be tweaked, or you can see how the screenshot looks when that command is commented out.

Edited by GMK
Clarification, Optimization
Posted

In non-headless mode, it performs the screenshot, but not the full page.  I have not tried _WD_Screenshot or _WD_PrintToPDF for this purpose, but I'll play around with it.  Thanks!

Posted

After trying _WD_Screenshot, I see that I can just combine the element action and the Base 64 decode into one command.  With _WD_PrintToPDF, I do not need to mess with resizing the window to the document size before printing to PDF.  Unfortunately, I'm not aware of a way to get _WD_PrintToPDF to output to anything other than a PDF format.

Posted

I've been playing around with this in non-headless mode (on multiple browsers) and I'm finding that it just works better in headless mode.  The resizing of the window to fit the document isn't working the same in non-headless mode as in headless mode.

Posted

did you test in both modes what values are for:
 

window.innerWidth;
window.innerHeight;

?
 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Do you set your own print options or you use Default ?
 

_WD_PrintToPdf($sSession, $sOptions)

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

I just used default options for _WD_PrintToPDF.  I haven't taken the time to see how to format additional options (probably JSON, I'm guessing).  This is mostly because I'm not looking for a PDF solution here.

I figured the innerWidth and innerHeight had something to do with it, but I find that I don't need to be as concerned about it in headless mode. *shrug*

(Side note: I've also found that taking a full page screenshot is much more complicated when frames are involved.)

Posted

OK...testing window.innerWidth/Height values on a 1920x1080 window:

Non-Headless: 1904x992

Headless: 1920x1080

Posted (edited)

OK, I've had time to play a little more and came up with the following, which works best in headless mode for all browsers:

#include "wd_core.au3"
#include "wd_helper.au3"
#include "wd_capabilities.au3"

Global $bHeadless = True
Global $sCapabilities = SetupChrome($bHeadless)
_WD_Startup()

Global $sSession = _WD_CreateSession($sCapabilities)
If @error = $_WD_ERROR_Success Then
    Global $sURL = 'https://autoitscript.com/forum'
    _WD_Navigate($sSession, $sURL)
    _WD_LoadWait($sSession)
    Global $sFile = 'screenshot.png'
    _FullscreenScreenshot($sSession, $sFile)
    ShellExecuteWait($sFile)
EndIf
_WD_DeleteSession($sSession)
_WD_Shutdown()

Func SetupChrome($bHeadless)
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\chrome.log"')
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'chrome')
    _WD_CapabilitiesAdd('w3c', True)
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    If $bHeadless Then _WD_CapabilitiesAdd('args', '--headless')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupChrome

Func SetupEdge($bHeadless)
    _WD_Option('Driver', 'msedgedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\msedge.log"')
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'msedge')
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    If $bHeadless Then _WD_CapabilitiesAdd('args', '--headless')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupEdge

Func SetupGecko($bHeadless)
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--log trace')
    _WD_Option('Port', 4444)
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'firefox')
    _WD_CapabilitiesAdd('browserName', 'firefox')
    _WD_CapabilitiesAdd('acceptInsecureCerts', True)
    If $bHeadless Then _WD_CapabilitiesAdd('args', '--headless')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupGecko

Func SetupOpera($bHeadless)
    _WD_Option('Driver', 'operadriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\opera.log"')
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'opera')
    _WD_CapabilitiesAdd('w3c', True)
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    _WD_CapabilitiesAdd('binary', _WD_GetBrowserPath("opera"))
    If $bHeadless Then _WD_CapabilitiesAdd('args', '--headless')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupOpera

Func _FullscreenScreenshot($sSession, $sFile)
    _WD_Window($sSession, 'rect', '{"height":1080, "width":1920, "x":0, "y":0}')
    Local $asElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[contains(@class, 'container') or contains(@id, 'container') or contains(@name, 'container')]", Default, True)
    Local $iDIVWidth = 0, $oRect
    For $iElement = 0 To UBound($asElements) - 1
        $oRect = _WD_ElementAction($sSession, $asElements[$iElement], 'rect')
        If $oRect.Item('width') > $iDIVWidth Then $iDIVWidth = $oRect.Item('width')
    Next
    _WD_Window($sSession, 'rect', '{"height":1080, "width":' & $iDIVWidth & ', "x":0, "y":0}')
    Local $iHeight = _WD_ExecuteScript($sSession, "return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.body.clientHeight, document.documentElement.clientHeight)", Default, Default, $_WD_JSON_Value)
    If Not $bHeadless Then $iHeight += 88
    Local $iWidth = _WD_ExecuteScript($sSession, "return Math.max(document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth, document.body.clientWidth, document.documentElement.clientWidth)", Default, Default, $_WD_JSON_Value)
    If $iDIVWidth > $iWidth Then $iWidth = $iDIVWidth
    If Not $bHeadless Then $iWidth += 16
    Local $sBodyElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByTagName, 'body')
    $oRect = _WD_ElementAction($sSession, $sBodyElement, 'rect')
    Local $iExpand = $iWidth - $oRect.Item('width')
    _WD_Window($sSession, 'rect', '{"height":' & $iHeight & ', "width":' & $iWidth + $iExpand & ', "x":0, "y":0}')
    Local $dResponse = _WD_Screenshot($sSession, $sBodyElement, 2)
    Local $hFileOpen = FileOpen($sFile, $FO_BINARY + $FO_OVERWRITE)
    FileWrite($hFileOpen, $dResponse)
    FileClose($hFileOpen)
EndFunc  ;==>_FullscreenScreenshot

 

Edited by GMK
Removed unnecessary lines

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...