使用 VBA 访问 iframe 中的元素 [英] Access elements inside iframe using VBA

查看:252
本文介绍了使用 VBA 访问 iframe 中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问使用 Wix 站点生成器制作的网页上的 iframe 元素,使用 PowerPoint VBA.

I'm trying to access the elements of an iframe, on a webpage that was made using the Wix Site Builder, using PowerPoint VBA.

我尝试了在 Google 和其他网页上找到的所有内容,但我无法弄清楚.最常见的错误是我尝试使用 contentDocument 时的自动化错误"和我尝试使用 contentWindow 时的访问被拒绝".

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"

我正在尝试使用 iframe 内的 idinput"更改输入的值,它没有类或 id.上面的代码是我尝试的最后一个抛出错误自动化错误"的代码.

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".

推荐答案

我认为您会遇到 IE 的同源策略问题.你可以获取 iframe 的 src 并导航到那个

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

<小时>

使用 selenium basic 和 chrome 来解决同源策略问题.安装 selenium basic 后,必须添加对 selenium 类型库的引用 vbe > tools > references > selenium type library


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

这篇关于使用 VBA 访问 iframe 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆