簡體   English   中英

如何使用 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 ou C 與這些元素交互,但從未與 VBA 交互。 你能幫忙把這些命令翻譯成 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

谷歌代碼頁:

在此處輸入圖片說明

這是確保腳本將等待下載完成的簡單方法。

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

結束子

公共函數 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(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))

結束函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM