我在具有以下域的页面上
https://my.example.com
当我在 chrome 控制台中键入以下内容时:
document.querySelector('.my-app-iframe').contentDocument我得到null因为 iframesrc是https://members.example.com
使用标志启动 chrome(使用 nightwatch.js 测试运行程序)--disable-web-security解决了这个问题。我现在可以访问contentDocumentiframe 并与内容交互。
但后来发生了这种情况:
document.querySelector('.my-app-iframe'):
<iframe src='https://members.example.com'> //all good
var myFrame = document.querySelector('.my-app-iframe').contentDocument.querySelector('#iframe1'):
<iframe id='myContent' src='/Content/myApp.aspx'>
myFrame.contentDocument.querySelector('#iframe2')
<iframe src='https://my.example.com/index.html'>
现在我想访问最后一个嵌套的 iframe,但我得到了这个:
myFrame.contentDocument.querySelector('#iframe2').contentDocument.querySelector('#iframe3')
Uncaught DOMException: Blocked a frame with origin "https://my.example.com" from accessing a cross-origin frame.
这很奇怪,因为我从位于的网站开始my.example.com,并且由于我禁用了网络安全,我可以访问具有不同来源的 iframe。但是后来我无法访问与我开始的域相同的另一个嵌套 iframe。为什么会发生这种情况,什么可能是解决方案?