Jump to content

Recommended Posts

  • 2 months later...
Posted

Finally I was able to attach to runing Chrome instances.

Here is example script:

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

# HOW TO TEST:
; At first run choose [Yes] to create new session Chrome running instance
; At second run choose [No] to attach to active Chrome running instance

Global $_MY__WD_SESSION
Global $__g_sDownloadDir = ""

_Test()

Exit

Func _Test()
    Local $iAnswer = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON2, "Question", _
            "Open new sesion ?" & @CRLF & "[ NO ] = Try attach to active Chrome instance")
    If $iAnswer = $IDYES Then
        _Testing_CreateSession()
        Return     ; do not process next functions
    Else
        _Testing_AttachSession()
        _WD_Navigate($_MY__WD_SESSION, 'https://www.google.com/')
    EndIf

    $iAnswer = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON2, "Question", _
            "Do you want to test ?" & @CRLF & "[ NO ] = Refresh - prevent expiration")
    If $iAnswer = $IDYES Then
        _Testing_WD_Navigate()
    Else
        _Testing_Refreshing()
    EndIf

    ; CleanUp
    _WD_DeleteSession($_MY__WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Test

Func _Testing_CreateSession()
    $_MY__WD_SESSION = _MY__WD_SetupChrome(False, $__g_sDownloadDir, False)
EndFunc   ;==>_Testing_CreateSession

Func _Testing_AttachSession()
    $_MY__WD_SESSION = _MY__WD_SetupChrome(False, $__g_sDownloadDir, True)
EndFunc   ;==>_Testing_AttachSession

Func _Testing_Refreshing()
    While 1
;~      _WD_Navigate($_MY__WD_SESSION, '')
        _WD_Action($_MY__WD_SESSION, 'REFRESH')
        Local $iAnswer = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON2, "Question", "Finish refreshing?" & @CRLF & "[No] = Refresh - prevent expiration", 60)
        If $iAnswer = $IDYES Then Return
    WEnd
EndFunc   ;==>_Testing_Refreshing

Func _Testing_WD_Navigate()
    _WD_Navigate($_MY__WD_SESSION, 'https://www.autoitscript.com/forum')
EndFunc   ;==>_Testing_WD_Navigate

Func _MY__WD_SetupChrome($b_Headless, $s_Download_dir = Default, $bTryAttach = False)
    If $s_Download_dir = Default Then
        $s_Download_dir = ''
    ElseIf $s_Download_dir Then
        If FileExists($s_Download_dir) = 0 Then $s_Download_dir = ''
    EndIf

    _WD_UpdateDriver('chrome')
    If @error Then Return SetError(@error, @extended, '')

    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_CapabilitiesStartup()
;~  Local $s_AttachOption = (($bTryAttach) ? ("") : (" --remote-debugging-port=9222"))
;~  _WD_Option('DriverParams', '--log trace' & $s_AttachOption)
    _WD_CapabilitiesAdd('firstMatch', 'chrome')
    _WD_CapabilitiesAdd('w3c', True)
    _WD_CapabilitiesAdd('detach', False)
    _WD_CapabilitiesAdd('binary', "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")

    If $bTryAttach Then
            _WD_CapabilitiesAdd('debuggerAddress', '127.0.0.1:9222')
    Else
            _WD_CapabilitiesAdd('args', '--remote-debugging-port=9222')
    EndIf
    If $b_Headless Then _
            _WD_CapabilitiesAdd('args', '--headless')
    If $s_Download_dir Then _
            _WD_CapabilitiesAdd('prefs', 'download.default_directory', $s_Download_dir)

    _WD_CapabilitiesDump(@ScriptLineNumber & ' :WebDriver:Capabilities:')

    Local $iWebDriverPID = _WD_Startup()
    If @error Then Return SetError(@error, @extended, '')



    Local $s_Capabilities = _WD_CapabilitiesGet()
    Local $WD_SESSION = _WD_CreateSession($s_Capabilities)
    If @error Then Return SetError(@error, @extended, $WD_SESSION)

    Local $iBrowserPID = _WD_GetBrowserPID($iWebDriverPID, 'chrome')
    ConsoleWrite("! $iBrowserPID=" & $iBrowserPID & @CRLF)

    Return SetError(@error, @extended, $WD_SESSION)
EndFunc   ;==>_MY__WD_SetupChrome


; #FUNCTION# ====================================================================================================================
; Name ..........: _WD_GetBrowserPID
; Description ...: Get the PID of the browser that was launched by WebDriver
; Syntax ........: _WD_GetBrowserPID($iWebDriverPID, $sBrowserName)
; Parameters ....: $iWebDriverPID       - WebDriver PID returned by _WD_Startup()
;                  $sBrowserName        - [optional] Browser name from the list of supported browsers ($_WD_SupportedBrowsers)
; Return values .: Success - Browser PID
;                  Failure - 0 and sets @error to one of the following values:
;                  - $_WD_ERROR_GeneralError
;                  - $_WD_ERROR_NotSupported
;                  - $_WD_ERROR_NoMatch
; Author ........: mLipok
; Modified ......: Danp2
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WD_GetBrowserPID($iWebDriverPID, $sBrowserName = '')
    Local Const $sFuncName = "_WD_GetBrowserPID"
    Local $iErr = $_WD_ERROR_Success, $iExt = 0, $iIndex = 0, $sBrowserExe = '', $aProcessList, $iBrowserPID = 0, $sMessage = ''
    Local $sDriverProcessName = _WinAPI_GetProcessName($iWebDriverPID)

    If @error Or Not ProcessExists($iWebDriverPID) Then
        #REMARK ProcessExists($iWebDriverPID) is required because of ; https://www.autoitscript.com/trac/autoit/ticket/3894
        $sDriverProcessName = ''
        $iErr = $_WD_ERROR_GeneralError
        $iExt = 1
        $sMessage = 'Unable to retrieve WebDriver process name for given PID'
    ElseIf _ArraySearch($_WD_SupportedBrowsers, $sDriverProcessName, Default, Default, Default, Default, Default, $_WD_BROWSER_DriverName) = -1 Then
        $iErr = $_WD_ERROR_NotSupported
        $sMessage = 'WebDriverPID is related to not supported WebDriver exe name'
    Else
        If $sBrowserName Then
            $iIndex = _ArraySearch($_WD_SupportedBrowsers, $sBrowserName, Default, Default, Default, Default, Default, $_WD_BROWSER_Name)
        EndIf
        If @error Then
            $iErr = $_WD_ERROR_GeneralError
            $iExt = 2
            $sMessage = 'BrowserName can not be found on supported browsers names list'
        Else
            $aProcessList = _WinAPI_EnumChildProcess($iWebDriverPID)
            If @error Then
                $iErr = $_WD_ERROR_GeneralError
                $iExt = 3
                $sMessage = 'Session was not created properly'
            Else
                _ArrayDisplay($aProcessList, '$aProcessList')
                ; all not supported EXE file names should be removed from $aProcessList, for example "conhost.exe" can be used by WebDriver exe file
                For $iCheck = $aProcessList[0][0] To 1 Step -1
                    _ArraySearch($_WD_SupportedBrowsers, $aProcessList[$iCheck][1], Default, Default, Default, Default, Default, $_WD_BROWSER_ExeName)
                    If @error Then
                        _ArrayDelete($aProcessList, $iCheck)
                        $aProcessList[0][0] -= 1
                    EndIf
                Next
                If $aProcessList[0][0] = 0 Then
                    $iErr = $_WD_ERROR_GeneralError
                    $iExt = 4
                    $sMessage = 'All child process (file names) are not listed on supported browsers exe'
                EndIf
            EndIf
        EndIf
    EndIf

    If $iErr = $_WD_ERROR_Success Then
        If $sBrowserName = '' Then
            $iBrowserPID = $aProcessList[1][0]
        Else
            $sBrowserExe = $_WD_SupportedBrowsers[$iIndex][$_WD_BROWSER_ExeName]
            For $i = 1 To $aProcessList[0][0]
                If $aProcessList[$i][1] = $sBrowserExe Then
                    $iBrowserPID = $aProcessList[$i][0]
                    $sMessage = "Browser - " & $aProcessList[$i][1] & " - PID = " & $iBrowserPID
                    ExitLoop
                EndIf
            Next
            If Not $iBrowserPID Then
                $iErr = $_WD_ERROR_NoMatch
                $sMessage = 'BrowserExe related to requested BrowserName was not matched in the webdriver child process list'
            EndIf
        EndIf
    EndIf
    Return SetError(__WD_Error($sFuncName, $iErr, $sMessage, $iExt), $iExt, $iBrowserPID)
EndFunc   ;==>_WD_GetBrowserPID

 

As so far the main issue was that we was trying to use:

_WD_Option('DriverParams', '--log trace  --remote-debugging-port=9222')

but it should be :

_WD_CapabilitiesAdd('args', '--remote-debugging-port=9222')

 

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

@SkysLastChance is it works for you ?

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

  • 1 month later...
  • 4 months later...
Posted (edited)
  On 3/10/2022 at 8:45 AM, mLipok said:

Here is better example showing how to attach to firstly opened FireFox session/instance:

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

# HOW TO TEST:
; At first run choose [Yes] to create new session FireFox running instance
; At second run choose [No] to attach to active FireFox running instance

# TODO:
; https://github.com/operasoftware/operachromiumdriver/blob/master/docs/desktop.md   --remote-debugging-port=port

Global $_MY__WD_SESSION
Global $__g_sDownloadDir = @ScriptDir & '\Testing_Download'

_Test()

Exit

Func _Test()
    Local $s_FireFox_Binary = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
    If $s_FireFox_Binary And FileExists($s_FireFox_Binary) = 0 Then $s_FireFox_Binary = ''

    Local $iAnswer = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON2, "Question", _
            "Open new sesion ?" & @CRLF & "[ NO ] = Try attach to active FireFox instance")
    If $iAnswer = $IDYES Then
        _Testing_CreateSession($s_FireFox_Binary)
;~      _Testing_WD_Navigate()
        Return     ; do not process next functions
    Else
        _Testing_AttachSession($s_FireFox_Binary)
        _WD_Navigate($_MY__WD_SESSION, 'https://www.google.com/')
    EndIf

    $iAnswer = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON2, "Question", _
            "Do you want to test ?" & @CRLF & "[ NO ] = Refresh - prevent expiration")
    If $iAnswer = $IDYES Then
        _Testing_WD_Navigate()
    Else
        _Testing_Refreshing()
    EndIf

    ; CleanUp
    _WD_DeleteSession($_MY__WD_SESSION)
    _WD_Shutdown()
EndFunc   ;==>_Test

Func _Testing_CreateSession($s_FireFox_Binary)
    $_MY__WD_SESSION = _MY__WD_SetupFireFox(False, $__g_sDownloadDir, $s_FireFox_Binary, False)
EndFunc   ;==>_Testing_CreateSession

Func _Testing_AttachSession($s_FireFox_Binary)
    $_MY__WD_SESSION = _MY__WD_SetupFireFox(False, $__g_sDownloadDir, $s_FireFox_Binary, True)
EndFunc   ;==>_Testing_AttachSession

Func _Testing_Refreshing()
    While 1
;~      _WD_Navigate($_MY__WD_SESSION, '')
        _WD_Action($_MY__WD_SESSION, 'REFRESH')
        Local $iAnswer = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON2, "Question", "Finish refreshing?" & @CRLF & "[No] = Refresh - prevent expiration", 60)
        If $iAnswer = $IDYES Then Return
    WEnd
EndFunc   ;==>_Testing_Refreshing

Func _MY__WD_SetupFireFox($b_Headless, $s_Download_dir = Default, $s_FireFox_Binary = Default, $bTryAttach = False)
    If $s_Download_dir = Default Then
        $s_Download_dir = ''
    ElseIf $s_Download_dir Then
        If FileExists($s_Download_dir) = 0 Then $s_Download_dir = ''
    EndIf

    Local $s_Capabilities = Default
    Local $s_Profile_Dir  = @LocalAppDataDir & '\Mozilla\Firefox\Profiles\WD_Testing_Profile'
    DirCreate($s_Download_dir) ; MUST EXIST !!
    DirCreate($s_Profile_Dir) ; MUST EXIST !!

    _WD_UpdateDriver('firefox')
    If @error Then Return SetError(@error, @extended, '')

    #WARRNING DO NOT USE '--log-path=' BECAUSE OF   RODO / GDPR
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('Port', 4444)
    _WD_Option('DefaultTimeout', 1000)

    If $bTryAttach Then
        _WD_Option('DriverParams', '--log trace --marionette-port 2828 --connect-existing')
    Else
        _WD_Option('DriverParams', '--log trace --marionette-port 2828')

        _WD_CapabilitiesStartup()
        _WD_CapabilitiesAdd('alwaysMatch')
        _WD_CapabilitiesAdd('acceptInsecureCerts', True)
        _WD_CapabilitiesAdd('firstMatch', 'firefox')
        _WD_CapabilitiesAdd('args', '-profile')

        _WD_CapabilitiesAdd('args', $s_Profile_Dir) ; CHANGE TO PROPER DIRECTORY PATH
        If $s_FireFox_Binary Then _WD_CapabilitiesAdd('binary', $s_FireFox_Binary)

        If $b_Headless Then _
                _WD_CapabilitiesAdd('args', '--headless')

        _WD_CapabilitiesDump(@ScriptLineNumber & ' :WebDriver:Capabilities:')
        $s_Capabilities = _WD_CapabilitiesGet()
    EndIf

    _WD_Startup()
    If @error Then Return SetError(@error, @extended, '')

    Local $WD_SESSION = _WD_CreateSession($s_Capabilities)
    Return SetError(@error, @extended, $WD_SESSION)
EndFunc   ;==>_MY__WD_SetupFireFox

Func _Testing_WD_Navigate()
    _WD_Navigate($_MY__WD_SESSION, 'https://www.autoitscript.com/forum')
EndFunc   ;==>_Testing_WD_Navigate


REMARK:
It turned out to be very useful when implementing the automation of websites for which login is required (especially with OTP send via SMS ), or in a situation where you need to repeatedly test one selected aspect of automation.

Expand  

 

Hello @mLipok

Does this work with windows11/firefox x64? My current firefox version is 109.0.1 (64-bit)

When I run this code, it gives me this error in geckodriver and is unable to attach to an existing firefox instance:

"webdriver::command      WARN    You are using deprecated legacy session negotiation patterns (desiredCapabilities/requiredCapabilities), see https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#Legacy"

 

Alternatively, do you know how to enable adblock extensions when using webdriver?

Edited by Ganreizu
Posted
  On 2/6/2023 at 7:18 PM, Ganreizu said:

When I run this code, it gives me this error in geckodriver and is unable to attach to an existing firefox instance:

Expand  

That isn't an error. It is just a warning display in the webdriver console, and it is the result of calling _WD_CreateSession without supplying a Capabilities string.

FWIW, the code ran fine for me.

Posted
  On 2/6/2023 at 8:00 PM, Danp2 said:

it is the result of calling _WD_CreateSession without supplying a Capabilities string

Expand  

but this should'nt be done this way.

@Ganreizu please try to add Default value like here:

If $bTryAttach Then
        _WD_Option('DriverParams', '--log trace --marionette-port 2828 --connect-existing')
        $s_Capabilities = Default
    Else

 

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 (edited)

Oh I see

Is it an issue with gecko WebDriver implementation ?

Edited by mLipok

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

Yeah but for now {} are properly writen capabilities.

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

  • 2 weeks later...
Posted (edited)

I just update wiki in tools section:  https://www.autoitscript.com/wiki/WebDriver#Tools

by adding tools:    https://github.com/openstyles/stylus

Thanks to this tool, together with  https://selectorshub.com/     you can easily develop your own style for your specyfic page.

For example I use it to change how web page is displayed to have a pure black/white web page without headers, footers, buttons, only desired data, and then I put them to PDF and store in SQL.

This is advance usage, but I'm wondering do you want a new function to inject CSS formating into web page ?

Edited by mLipok

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

  • 1 month later...
Posted

I just started creating a little more complete and easy to use template test script

https://github.com/mlipok/Au3WebDriver-testing

 

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

  • 3 weeks later...
Posted
  On 4/7/2022 at 5:59 PM, OldMan-Newbee said:

Do you know of or have an example of connecting to an existing MSEdge browser session?

Expand  

Similar to :

 

 

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

  • 4 months later...
Posted (edited)

wd_GettingStarted.au3:

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

_Example()

Func _Example()
    # REMARK
    #   This is not functional script
    #   It only shows the concept how to use WebDriver UDF

    #Region ; initialize webdriver sesion

    ; you should take care about download/update dirver
    If $IDYES = MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONQUESTION + $MB_DEFBUTTON1, "Question", _
            "Do you want to download/update driver ?") Then
        _WD_UpdateDriver('chrome')
    EndIf

    ; specify driver, port and other options
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--verbose --log-path="' & @ScriptDir & '\chrome.log"')

    ; start the driver
    _WD_Startup()
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's

    ; create capabilites for session
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'chrome')
    _WD_CapabilitiesAdd('w3c', True)
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    Local $sCapabilities = _WD_CapabilitiesGet()

    ; create session with given Capabilities
    Local $WD_SESSION = _WD_CreateSession($sCapabilities)
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's

    #EndRegion ; initialize webdriver sesion

    #Region ; do your's stuff

    ; navigate to some website
    Local $sURL = '******'
    _WD_Navigate($WD_SESSION, $sURL)
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's

    ; wait for loading process ends
    _WD_LoadWait($WD_SESSION, 1000)
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's

    ; for example find element
    Local $sXPath = "*****"
    Local $sElement = _WD_FindElement($WD_SESSION, $_WD_LOCATOR_ByXPath, $sXPath)
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's

    ; get element text
    Local $sText = _WD_ElementAction($WD_SESSION, $sElement, 'text')
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's
    ConsoleWrite($sText & @CRLF)

    ; or click the element
    _WD_ElementAction($WD_SESSION, $sElement, 'click')
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's

    #EndRegion ; do your's stuff

    #Region ; Clean Up

    ; on the end session should be deleted
    _WD_DeleteSession($WD_SESSION)
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's

    ; and driver should be closed
    _WD_Shutdown()
    If @error Then Return SetError(@error, @extended, 0) ; always remember to check and handle error's

    #EndRegion ; Clean Up

EndFunc   ;==>_Example


ADDED to: https://www.autoitscript.com/wiki/WebDriver#Getting_started_example

wd_GettingStarted.au3Fetching info...

Edited by mLipok

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

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
×
×
  • Create New...