Jump to content
Exit

[Solved] How to attach to an already running Chrome instance ?

Recommended Posts

Edit: Solved using UIAWrappers UDF.  See 4th post in this thread.

====================================================

I'm new to Chrome browser and WebDriver UDF.

I want to control an existing instance of chrome browser, but it fails.

I dont know how to attach to an existing instance.

Is it possible?

This code runs perfectly since the browser instance is created by WebDrver UDF:

#include "wd_core.au3"
#include "wd_helper.au3"
_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')
_WD_Startup()
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true }}}}'
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession, "http://www.h--e.de")
Sleep(2000) ; time to see the login page
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='kennwort']")
_WD_ElementAction($sSession, $sElement, "value", "Autoit")
MsgBox(64 + 262144, Default, "Press OK to exit", 0)
_WD_DeleteSession($sSession)
_WD_Shutdown()

But when I try to attach to an existing instance, it fails:

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

$pid = ShellExecute("chrome.exe", "http://h--e.de") ;  create a chrome browser instance to simulate a already existing page

_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')
_WD_Startup()
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true }}}}'
$sSession = _WD_CreateSession($sDesiredCapabilities)

_WD_Attach($sSession, "Login")  ; try to attach to the already started instance. Does NOT function  :-(
Sleep(2000)

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='kennwort']")
_WD_ElementAction($sSession, $sElement, "value", "Autoit")
MsgBox(64 + 262144, Default, "Press OK to exit", 0)
_WD_DeleteSession($sSession)
_WD_Shutdown()
ProcessClose($pid)

How can I connect/attach to an existing instance?

 

TIA Exit

 

Edited by Exit
Set thread solved

App: Au3toCmd              UDF: _SingleScript()                             

Share this post


Link to post
Share on other sites

Moved to the appropriate forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
On 3/13/2019 at 1:03 AM, Danp2 said:

This has come up before in the support thread. AFAIK, while you can do this with Firefox, this isn't possible with Chrome.

 

It is possible with Chrome :) 

You just have to elevate Chrome browser to force accessibility

#include "UIAWrappers.au3"

If Not ProcessExists("chrome.exe") Then
    $pid = ShellExecute("chrome.exe", "h--e.de")
    Sleep(500)
EndIf

$oLogin = _UIA_getFirstObjectOfElement($UIA_oDesktop, "name:=Login - Google Chrome", $treescope_subtree)
If Not IsObj($oLogin) Then Exit MsgBox(16 + 262144, Default, "No Login Page found. Press OK to exit.", 0)
$oPassword = _UIA_getFirstObjectOfElement($oLogin, "title:=Enter your Id", $treescope_subtree)
If Not IsObj($oPassword) Then
    $pid = ShellExecute("chrome.exe", " about:blank ")
    Sleep(100)
    $oBlamk = _UIA_getFirstObjectOfElement($UIA_oDesktop, "name:=about:blank - Google Chrome", $treescope_subtree)

    Switch @OSLang
        Case "0407" ; German - Germany
            $oAddressBar = _UIA_getFirstObjectOfElement($oBlamk, "title:=Adress- und Suchleiste", $treescope_subtree)
        Case "0409" ; English - United States
            $oAddressBar = _UIA_getFirstObjectOfElement($oBlamk, "title:=Address and search bar", $treescope_subtree)
        Case "0413" ; Dutch - Netherlands
            $oAddressBar = _UIA_getFirstObjectOfElement($oBlamk, "title:=Adres- en zoekbalk", $treescope_subtree)
        Case "040c" ; French - France
            $oAddressBar = _UIA_getFirstObjectOfElement($oBlamk, "title:=Barre d'adresse et de recherche", $treescope_subtree)
        Case "040A" ; Spanish - Spain
            $oAddressBar = _UIA_getFirstObjectOfElement($oBlamk, "title:=Barra de direcciones y de búsqueda", $treescope_subtree)
        Case "0410" ; Italian - Italy
            $oAddressBar = _UIA_getFirstObjectOfElement($oBlamk, "title:=Barra degli indirizzi e di ricerca", $treescope_subtree)
        Case Else
            Exit MsgBox(16 + 262144, Default, "Address bar title not found due to unsupported language. Press OK to exit", 0)
    EndSwitch

    Sleep(100)
    _UIA_action($oAddressBar, "setfocus")
;~  chrome://accessibility  "--force-renderer-accessibility"
    _UIA_action($oAddressBar, "setvalue using keys", "chrome://accessibility{ENTER}")
    $oAccess = _UIA_getFirstObjectOfElement($UIA_oDesktop, "name:=Accessibility Internals - Google Chrome", $treescope_subtree)
    _UIA_action($oAccess, "setvalue using keys", "{TAB}{Space}{up}{down}{TAB}{TAB}{Space}^w")
    $oLogin = _UIA_getFirstObjectOfElement($UIA_oDesktop, "name:=Login - Google Chrome", $treescope_subtree)
    _UIA_action($oLogin, "setfocus")
    $oPassword = _UIA_getFirstObjectOfElement($oLogin, "title:=Enter your Id", $treescope_subtree)
EndIf
Sleep(100)
_UIA_action($oPassword, "setfocus")
_UIA_action($oPassword, "sendkeys", "autoit")
Sleep(1000)
MsgBox(64 + 262144, Default, "AutoIt index page loaded successful. Press OK to exit and run the script again. It will run faster since the accessability enviroment is already loadad. Don't close the LOGIN window.", 0)
_UIA_action($oLogin, "sendkeys", "!{left}")
Exit
; End of Script

Drop me a note if you need it in another language.

Edited by Exit
added langues

App: Au3toCmd              UDF: _SingleScript()                             

Share this post


Link to post
Share on other sites

 

1 hour ago, Danp2 said:

@Exit Can you explain (or even better show in code) how this would allow an existing Chrome instance to be "attached" via the Webdriver UDF?

It seems not possible with the Webdriver UDF, 🙁  but with the UIAWrappers UDF 🙂

See my sample above. 

Since I need to work with already running Chrome instances, I will use UIAWrappers UDF fom now on.

 :ILA2:


App: Au3toCmd              UDF: _SingleScript()                             

Share this post


Link to post
Share on other sites

I found a simple solution to reconnect to an existing chromedriver.  Once the driver is loaded, the "session id" is returned.  Subsequent communication is via TCP to the defined port while passing in the session id.

Global $sSessionFileName = "Session.ID"
    
    $sSession = _WD_CreateSession($sDesiredCapabilities)
    If @error <> $_WD_ERROR_Success Then
        Exit -1
    EndIf

    FileWrite($sSessionFileName, $sSession & @CRLF)

What I've done is to write out to a file the value of the sessionid following the call to _WD_CreateSession.  Then when restarting an new app, I would call the following:

Func _WD_ReAttach($Type)
    Local Const $sFuncName = "_WD_ReAttach"

    Switch $Type
        Case $eFireFox
            SetupFireFox()

        Case $eChrome
            SetupChrome()

        Case $eEdge
            SetupEdge()

    EndSwitch

    Local $pFile = FileOpen($sSessionFileName, 0)
    If (@error = 0) Then
        $sSession = FileReadLine($pFile)
    EndIf
EndFunc   ;==>_WD_ReAttach

Which sets up the _WD_Option then reads in the saved session string.

I can then continue interacting with the Chrome driver....

 

John W.

Share this post


Link to post
Share on other sites

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...