相关疑难解决方法(0)

SecurityError:阻止具有原点的帧访问跨源帧

我正在<iframe>我的HTML页面中加载一个并尝试使用Javascript访问其中的元素,但是当我尝试执行我的代码时,我收到以下错误:

SecurityError: Blocked a frame with origin "http://www.<domain>.com" from accessing a cross-origin frame.
Run Code Online (Sandbox Code Playgroud)

你能帮我找一个解决方案,以便我可以访问框架中的元素吗?

我正在使用此代码进行测试,但徒劳无功:

$(document).ready(function() {
    var iframeWindow = document.getElementById("my-iframe-id").contentWindow;

    iframeWindow.addEventListener("load", function() {
        var doc = iframe.contentDocument || iframe.contentWindow.document;
        var target = doc.getElementById("my-target-id");

        target.innerHTML = "Found it!";
    });
});
Run Code Online (Sandbox Code Playgroud)

javascript iframe jquery same-origin-policy

487
推荐指数
4
解决办法
82万
查看次数

Puppeteer iframe contentFrame 返回 null

我有一个有趣的木偶问题,我不知道如何解决。

我有一个网页,上面有一个iframe。要获得 iframe 句柄,我执行以下操作:

const iframeHandle = await page.$('iframe')
Run Code Online (Sandbox Code Playgroud)

要获取 contentFrame 我只需运行:

const frame = await iframeHandle.contentFrame()
Run Code Online (Sandbox Code Playgroud)

但是,这将返回null。

我打印出来iframeHandle以确保我得到了正确的元素,它确实抓取了正确的 iframe:

console.dir(iframeHandle)
Run Code Online (Sandbox Code Playgroud)
 _remoteObject: {
    type: 'object',
    subtype: 'node',
    className: 'HTMLIFrameElement',
    description: 'iframe',
    objectId: '{"injectedScriptId":5,"id":6}'
  },
Run Code Online (Sandbox Code Playgroud)

有谁知道如何获取 iframe 的内容?

javascript puppeteer

5
推荐指数
1
解决办法
1332
查看次数