0

I am trying to interact with element in the Chrome Download page (chrome://downloads/) to be able to find out when the download finishes.

But I can't interact with the elements in this page. For what I find out this is because of the shadow-root DOM elements.

I found in google some examples of how interact with these elements using java ou C, but never with VBA. Could you help to translate these comands to VBA?

https://medium.com/rate-engineering/a-guide-to-working-with-shadow-dom-using-selenium-b124992559f

https://medium.com/@alan.canlin/how-to-find-web-elements-in-shadow-doms-using-selenium-webdriver-and-c-36978f7de9ba

Google Code Page:

enter image description here

braX
  • 11,506
  • 5
  • 20
  • 33
naguall
  • 43
  • 1
  • 8
  • [Here](https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading/56570364#56570364) the code implementation to work with download page. And you can find detailed explanation on [here](https://stackoverflow.com/questions/56380091/how-to-interact-with-the-elements-within-shadow-root-open-while-clearing-brow/56381495#56381495) to know more about shadow-root dom. – supputuri Mar 24 '20 at 19:31

2 Answers2

0

Here is the simple method that will make sure the script will wait until the download is completed.

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
supputuri
  • 13,644
  • 2
  • 21
  • 39
0

Thank you very much! Works perfctly.

I am just posting here with some little modifications I needed to do:

Option Explicit

Sub 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

End Sub

Public Function 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

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

naguall
  • 43
  • 1
  • 8