问题
I'm trying to access the elements of an iframe, on a webpage that was made using the Wix Site Builder, using PowerPoint VBA.
I tried everything I found on Google and also other webpages but I can't figure it out. Most common errors are "Automation error" when I try to use contentDocument
and "Access Denied" when I try to use contentWindow
.
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://pptgamespt.wixsite.com/mppp/tests2"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Pausecode (2)
objIE.document.getElementsByTagName("iframe")(0).contentDocument.getElementById("input").Value = "some value"
I'm trying to change the value of the input with the id "input" that is inside the iframe, wich has no class or id. The code above is the last one I tried that throws the error "Automation error".
回答1:
I think you will hit same origin policy problems for IE. You can grab the src of the iframe and navigate on to that
Option Explicit
Public Sub SendInfo()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://pptgamespt.wixsite.com/mppp/tests2"
While .Busy Or .readyState < 4: DoEvents: Wend
.Navigate2 ie.document.querySelector("iframe").src
While .Busy Or .readyState < 4: DoEvents: Wend
.document.querySelector("#input").Value = "Bob"
.document.querySelector("#send").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Stop
.Quit
End With
End Sub
Using selenium basic and chrome to get around same origin policy problem. After installing selenium basic a reference to selenium type library must be added vbe > tools > references > selenium type library
Option Explicit
Public Sub EnterInfo()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "https://pptgamespt.wixsite.com/mppp/tests2"
With d
.Start "Chrome"
.get URL
.SwitchToFrame .FindElementByCss("iframe")
Do
Loop While .FindElementsByCss("#input").Count = 0
.FindElementByCss("#input").SendKeys "tada"
Stop
.Quit
End With
End Sub
来源:https://stackoverflow.com/questions/53951031/access-elements-inside-iframe-using-vba