与 ChromeDriver 和 MSXML2.ServerXMLHTTP (VBA) 交互时发送自定义用户数据目录
Send custom user-data-dir when interating with ChromeDriver and MSXML2.ServerXMLHTTP (VBA)
我正在尝试使用 ChromeWebDriver 和 VBA 上的 MSXML2.ServerXMLHTTP
打开 Google Chrome 的实例。该代码可以很好地打开浏览器的新实例,但是当我尝试向 WebDriver 发送自定义用户数据目录(存储在 D:\Profiles\tdmsoares\Desktop\tmpChromeUserData\User Data
中时,它不起作用:
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
备注:
- 当尝试使用
Debug.print client.responseText
从 MSXML2.ServerXMLHTTP
查看对象 client
的结果时,我得到:
{"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"}}
这意味着 user-data-dir 设置为 Temp,尽管尝试更改它
我在 W3.org 和 https://chromedriver.chromium.org/capabilities 查看了 WebDriver 的文档,但现在我仍然没有找到解决方案
我没有使用 Selenium(互联网上有很多资源)
对于那些面临同样问题的人:
问题是由于错误的 json 请求。我在再次阅读 W3.org 的 Webdriver 文档以及 Ish Abb 的这篇很棒的 post https://dev.to/rookieintraining/understanding-the-selenium-webdriver-o8o(谢谢!)并在 curl 上测试它然后在vba 本身使用 MSXML2.ServerXMLHTTP
对于以自定义用户数据目录开始 Google Chrome 的新会话请求(除了 url 之外,例如:http://localhost: 9515/会话)json 对象有(基于对我有用的):
{"capabilities":{"firstMatch":[{"goog:chromeOptions":{"args":["user-data-dir=Your path\\ToCustomProfile"]}}]}}
或
{"capabilities":{"alwaysMatch":{"goog:chromeOptions":{"args":["user-data-dir=Your path\\ToCustomProfile"]}}}}
简而言之,我必须考虑它的不同工作方式:
user-data-dir
没有双破折号“--”
- 定义
"alwaysMatch": {}"
或 "firstMatch":[]
记住 firstMatch
需要括号 [] 中的列表
- 在windows OS中我们需要在处理文件路径时转义\
user-data-dir
根据 Chrome 驱动程序文档,是 args
的成员,args
是 goog:chromeOptions
的成员
我正在尝试使用 ChromeWebDriver 和 VBA 上的 MSXML2.ServerXMLHTTP
打开 Google Chrome 的实例。该代码可以很好地打开浏览器的新实例,但是当我尝试向 WebDriver 发送自定义用户数据目录(存储在 D:\Profiles\tdmsoares\Desktop\tmpChromeUserData\User Data
中时,它不起作用:
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
备注:
- 当尝试使用
Debug.print client.responseText
从MSXML2.ServerXMLHTTP
查看对象client
的结果时,我得到:
{"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"}}
这意味着 user-data-dir 设置为 Temp,尽管尝试更改它
我在 W3.org 和 https://chromedriver.chromium.org/capabilities 查看了 WebDriver 的文档,但现在我仍然没有找到解决方案
我没有使用 Selenium(互联网上有很多资源)
对于那些面临同样问题的人:
问题是由于错误的 json 请求。我在再次阅读 W3.org 的 Webdriver 文档以及 Ish Abb 的这篇很棒的 post https://dev.to/rookieintraining/understanding-the-selenium-webdriver-o8o(谢谢!)并在 curl 上测试它然后在vba 本身使用 MSXML2.ServerXMLHTTP
对于以自定义用户数据目录开始 Google Chrome 的新会话请求(除了 url 之外,例如:http://localhost: 9515/会话)json 对象有(基于对我有用的):
{"capabilities":{"firstMatch":[{"goog:chromeOptions":{"args":["user-data-dir=Your path\\ToCustomProfile"]}}]}}
或
{"capabilities":{"alwaysMatch":{"goog:chromeOptions":{"args":["user-data-dir=Your path\\ToCustomProfile"]}}}}
简而言之,我必须考虑它的不同工作方式:
user-data-dir
没有双破折号“--”- 定义
"alwaysMatch": {}"
或"firstMatch":[]
记住firstMatch
需要括号 [] 中的列表
- 在windows OS中我们需要在处理文件路径时转义\
user-data-dir
根据 Chrome 驱动程序文档,是args
的成员,args
是goog:chromeOptions
的成员