首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用SeleniumBasic与chrome://downloads/中的Shadow-Root DOM元素进行交互

如何使用SeleniumBasic与chrome://downloads/中的Shadow-Root DOM元素进行交互
EN

Stack Overflow用户
提问于 2020-03-25 03:16:22
回答 2查看 562关注 0票数 0

我正在尝试与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

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

Google代码页:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-26 07:04:30

非常感谢!工作得很完美。

我只是在这里发布一些我需要做的小修改:

代码语言:javascript
复制
Option Explicit

子Accessing_ShadowRoot_Object()

代码语言:javascript
复制
'================================='
'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作为整数)

代码语言:javascript
复制
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函数

函数ElapsedTime(endTime作为日期,startTime作为日期)

代码语言:javascript
复制
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函数

票数 0
EN

Stack Overflow用户

发布于 2020-03-25 03:44:45

下面是一个简单的方法,它将确保脚本将等待,直到下载完成。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60837821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档