如何使用 SeleniumBasic (VBA) 与 chrome://downloads/ 中的 Shadow-Root DOM 元素交互
How use SeleniumBasic (VBA) to interact with Shadow-Root DOM element in the chrome://downloads/
我正在尝试与 Chrome 下载页面 (chrome://downloads/) 中的元素进行交互,以便能够了解下载何时完成。
但是我无法与此页面中的元素进行交互。我发现这是因为 shadow-root DOM 元素。
我在 google 中找到了一些如何使用 java 或 C 与这些元素交互的示例,但从未使用 VBA。你能帮忙把这些命令翻译成 VBA 吗?
https://medium.com/rate-engineering/a-guide-to-working-with-shadow-dom-using-selenium-b124992559f
Google 代码页:
这是确保脚本等待下载完成的简单方法。
Function getDownLoadedFileName(maxTimeInMins As int)
Dim startTime As Date
startTime = Now()
Dim downloadPercentage As int
Do While ElapsedTime(Now(),startTime) < maxTimeInMins
downloadPercentage = driver.execute_script( "return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value")
If (downloadPercentage = 100) Then
getDownLoadedFileName = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text")
End If
Loop
End Function
Function ElapsedTime(endTime As Date, startTime As Date)
Dim Interval As Date
' Calculate the time interval.
Interval = endTime - startTime
' Format and return the time interval in seconds.
ElapsedTime = Int(CSng(Interval * 24 * 3600))
End Function
非常感谢!完美运行。
我只是在这里发帖,需要做一些小的修改:
Option Explicit
子Accessing_ShadowRoot_Object()
'================================='
'Declaração Early-Binding:
'================================='
Dim Selenium As New ChromeDriver '
'================================='
Selenium.Start "chrome", "chrome://downloads"
Selenium.get "/"
Dim Nome_Download As String
Nome_Download = getDownLoadedFileName(Selenium, 10)
Debug.Print Nome_Download
结束子
Public 函数 getDownLoadedFileName(Driver As WebDriver, maxTimeInMins As Integer)
Dim startTime As Date
startTime = Now()
Dim downloadPercentage
Do While ElapsedTime(Now(), startTime) < maxTimeInMins
downloadPercentage = Driver.ExecuteScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#frb0').shadowRoot.querySelector('#progress').value")
Debug.Print downloadPercentage
If (downloadPercentage = 100) Then
getDownLoadedFileName = Driver.ExecuteScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text")
End If
DoEvents
Loop
结束函数
函数 ElapsedTime(结束时间为日期,开始时间为日期)
Dim Interval As Date
' Calculate the time interval.
Interval = endTime - startTime
' Format and return the time interval in seconds.
ElapsedTime = Int(CSng(Interval * 24 * 3600))
结束函数
我正在尝试与 Chrome 下载页面 (chrome://downloads/) 中的元素进行交互,以便能够了解下载何时完成。
但是我无法与此页面中的元素进行交互。我发现这是因为 shadow-root DOM 元素。
我在 google 中找到了一些如何使用 java 或 C 与这些元素交互的示例,但从未使用 VBA。你能帮忙把这些命令翻译成 VBA 吗?
https://medium.com/rate-engineering/a-guide-to-working-with-shadow-dom-using-selenium-b124992559f
Google 代码页:
这是确保脚本等待下载完成的简单方法。
Function getDownLoadedFileName(maxTimeInMins As int)
Dim startTime As Date
startTime = Now()
Dim downloadPercentage As int
Do While ElapsedTime(Now(),startTime) < maxTimeInMins
downloadPercentage = driver.execute_script( "return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value")
If (downloadPercentage = 100) Then
getDownLoadedFileName = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text")
End If
Loop
End Function
Function ElapsedTime(endTime As Date, startTime As Date)
Dim Interval As Date
' Calculate the time interval.
Interval = endTime - startTime
' Format and return the time interval in seconds.
ElapsedTime = Int(CSng(Interval * 24 * 3600))
End Function
非常感谢!完美运行。
我只是在这里发帖,需要做一些小的修改:
Option Explicit
子Accessing_ShadowRoot_Object()
'================================='
'Declaração Early-Binding:
'================================='
Dim Selenium As New ChromeDriver '
'================================='
Selenium.Start "chrome", "chrome://downloads"
Selenium.get "/"
Dim Nome_Download As String
Nome_Download = getDownLoadedFileName(Selenium, 10)
Debug.Print Nome_Download
结束子
Public 函数 getDownLoadedFileName(Driver As WebDriver, maxTimeInMins As Integer)
Dim startTime As Date
startTime = Now()
Dim downloadPercentage
Do While ElapsedTime(Now(), startTime) < maxTimeInMins
downloadPercentage = Driver.ExecuteScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#frb0').shadowRoot.querySelector('#progress').value")
Debug.Print downloadPercentage
If (downloadPercentage = 100) Then
getDownLoadedFileName = Driver.ExecuteScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text")
End If
DoEvents
Loop
结束函数
函数 ElapsedTime(结束时间为日期,开始时间为日期)
Dim Interval As Date
' Calculate the time interval.
Interval = endTime - startTime
' Format and return the time interval in seconds.
ElapsedTime = Int(CSng(Interval * 24 * 3600))
结束函数