Search code examples
vbagoogle-chromewebdrivermsxmlhttpbrowsercapabilities

Send custom user-data-dir when interating with ChromeDriver and MSXML2.ServerXMLHTTP (VBA)


I'm trying to open an instance of Google Chrome using ChromeWebDriver and MSXML2.ServerXMLHTTP on VBA. The code works fine to open a new instance of the browser, but when I try to send to the WebDriver a custom user-data-dir (stored in D:\Profiles\tdmsoares\Desktop\tmpChromeUserData\User Data it doesn't work:


Private Function SendRequest() As Dictionary
    Dim client As Object
    Set client = CreateObject("MSXML2.ServerXMLHTTP")   'ServerXMLHTTP
    
    client.Open "POST", "http://localhost:9515/session"
    
    client.setRequestHeader "Content-Type", "application/json"
    Dim strJsonRequest As String
    strJsonRequest = 'the comments bellow was made intentionally to show different ways I tried
    'strJsonRequest = "{""capabilities"":{""alwaysMatch:""{""args"":""[--user-data-dir=D:/Profiles/tdmsoares/Desktop/tmpChromeUserData/User Data]""},}""sessionId"":""""}"
    'strJsonRequest = "{""capabilities"":{""args"":""[--user-data-dir=D:\Profiles\tdmsoares\Desktop\tmpChromeUserData\User Data\]""},""sessionId"":""}"
    'strJsonRequest = "{""capabilities"":{},{""desiredCapabilities"":{""args"":""[--user-data-dir=D:\Profiles\tdmsoares\Desktop\tmpChromeUserData\User Data]""},""sessionId"":""""}
    'strJsonRequest = "{""capabilities"":{""userDataDir"":""D:\\Profiles\\tdmsoares\\Desktop\\tmpChromeUserData\\User Data\\]""},""sessionId"":""""}"
    'strJsonRequest = "{""capabilities"":{""goog:chromeOptions:args"":""[--[user-data-dir=D:\\Profiles\\tdmsoares\\Desktop\\tmpChromeUserData\\User Data]""},""sessionId"":""""}

    client.send strJsonRequest

    Do While client.readyState < 4
        DoEvents
    Loop

    Set SendRequest = JsonConverter.ParseJson(client.responseText)
End Function

Notes:

  1. When trying to see the result of object client from MSXML2.ServerXMLHTTP using Debug.print client.responseText I get:
{"value":{"capabilities":{"acceptInsecureCerts":false,"browserName":"chrome","browserVersion":"91.0.4472.124","chrome":{"chromedriverVersion":"91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462})","userDataDir":"D:\\Profiles\\tdmsoares\\AppData\\Local\\Temp\\scoped_dir7064_314199858"},"goog:chromeOptions":{"debuggerAddress":"localhost:52688"},"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platformName":"windows","proxy":{},"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify","webauthn:extension:largeBlob":true,"webauthn:virtualAuthenticators":true},"sessionId":"e29d73791d5eec0dfcf2f51426233979"}}

This means the user-data-dir is set to Temp although the atempts to change this

  1. I look at documentation of WebDriver at W3.org and https://chromedriver.chromium.org/capabilities but still now I didn't find a solution for this

  2. I'm not using Selenium (which have a lot of resources on internet)


Solution

  • For those who are facing the same issue:

    The problem was due to wrong json request. I figured it out when reading again the Webdriver documentation at W3.org and also this awesome post https://dev.to/rookieintraining/understanding-the-selenium-webdriver-o8o by Ish Abb (thanks!) and testing it on curl and then on vba itself using MSXML2.ServerXMLHTTP

    For a new session request aiming at start Google Chrome with a custom user-data-dir (besides the url be, for example: http://localhost:9515/session) the json object have (based what worked for me):

    {"capabilities":{"firstMatch":[{"goog:chromeOptions":{"args":["user-data-dir=Your path\\\ToCustomProfile"]}}]}}

    Or

    {"capabilities":{"alwaysMatch":{"goog:chromeOptions":{"args":["user-data-dir=Your path\\\ToCustomProfile"]}}}}

    So in short, the way it worked different I have to consider:

    • user-data-dir it is placed without the double dashes "--"
    • define "alwaysMatch": {}" or "firstMatch":[] remembering that firstMatch requires a list in brackets []
    • In windows OS we need escaping the \ when dealing with file path
    • user-data-dir according to Chrome Driver documentation, is a member of argswhich is a member of goog:chromeOptions