我正在尝试与Chrome下载页面(chrome://downloads/)中的元素进行交互,以便能够了解下载何时完成。
但我无法与此页面中的元素交互。据我所知,这是因为影子根DOM元素。
我在google中找到了一些如何使用java ou C与这些元素交互的示例,但从未使用过VBA。你能帮我把这些命令翻译成VBA吗?
https://medium.com/rate-engineering/a-guide-to-working-with-shadow-dom-using-selenium-b124992559f
Google代码页:

发布于 2020-03-26 07:04:30
非常感谢!工作得很完美。
我只是在这里发布一些我需要做的小修改:
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结束子对象
公共函数getDownLoadedFileName(驱动程序作为WebDriver,maxTimeInMins作为整数)
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
LoopEnd函数
函数ElapsedTime(endTime作为日期,startTime作为日期)
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函数
发布于 2020-03-25 03:44:45
下面是一个简单的方法,它将确保脚本将等待,直到下载完成。
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 Functionhttps://stackoverflow.com/questions/60837821
复制相似问题