我试图在VBA中使用Selenium Basic访问两个iframe内的HTML,因为IE在我们的机器上被阻止了,而Python等对我们来说是不可用的。
以前我可以这样访问html:
Dim IE As InternetExplorerMedium
Set IE = New InternetExplorerMedium
' actual website excluded as it is a work hosted website which requires login, etc.
website = "..."
IE.navigate (website)
Dim IEDocument As HTMLDocument
Set IEDocument = IE.document.getElementById(id1).contentDocument.getElementById(id2).contentDocument
从那里我可以访问所有我可以使用的HTML元素。
现在我正在尝试使用Selenium Basic:
Set cd = New Selenium.ChromeDriver
website = "..."
cd.Start baseUrl:=website
cd.Get "/"
Dim af1 As Selenium.WebElement, af2 As Selenium.WebElement
Set af1 = cd.FindElementById("CRMApplicationFrame")
Set af2 = af1.FindElementById("WorkAreaFrame1")
它可以工作到最后一行,因为它可以设置为"crmapplicationframework";id;然而,我无法进入它的内部。
我认为解决方案在于执行一点JavaScript,类似于这个视频:https://www.youtube.com/watch?v=phYGCGXGtEw
虽然我没有一个#ShadowDOM行,但我有一个#document行。
基于并尝试改编视频,我尝试了以下内容:
Set af2 = cd.ExecuteScript(Script:="return arguments[0].contentDocument", arguments:=af1 )
然而,这并没有起作用。
我还测试了:
Dim af1 As Selenium.WebElement
Set af1 = cd.FindElementById("CRMApplicationFrame")
call cd.SwitchToFrame (af1)
Debug.Print cd.PageSource
但是,SwitchToFrame行不会执行,并出现438错误:Object doesn't support this property or method.
任何关于我如何成功的建议或指导都将非常感激!Replace:
call cd.SwitchToFrame (af1)
:
cd.SwitchToFrame "CRMApplicationFrame"
你可以在Selenium VBA Excel中找到相关的详细讨论-在iframe内点击链接的问题