Access elements inside iframe using VBA

只谈情不闲聊 提交于 2020-01-02 09:30:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!