未捕获的DOMException。在列出页面中的iframes时,阻止了一个原点为 "http://localhost:8080 "的框架访问一个跨原点的框架。

8 人关注

我想在一个页面中列出所有 iframe 的名称,这样我就可以通过Selenium访问它们。

问题是, iframe 的名称每次都会改变,所以我需要循环浏览所有的代码。

我正在得到。

未捕获的DOMException。阻止了一个原点为" "的框架。 http://localhost:8080 "从访问一个跨源框架。

error when I try to loop over them using:

for (var f = 0; f < window.frames.length; f++) {
    console.log(window.frames[f].name)

是否有办法以不同的方式获得iframe的名称?

3 个评论
你是用 javascript 还是用其他语言?
I'm using javascript + selenium
javascript
selenium
iframe
same-origin-policy
TwoShorts
TwoShorts
发布于 2018-07-04
4 个回答
undetected Selenium
undetected Selenium
发布于 2018-07-04
已采纳
0 人赞同

这个错误信息...

Uncaught DOMException: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame.

...意味着WebDriver实例blocked从访问一个跨源框架。

同源政策 : 同源政策 restricts how a document or script loaded from one origin can interact with a resource from another origin. It is a 重要的安全机制用于隔离潜在的恶意文件。

跨源资源共享(CORS)

跨源资源共享(CORS) : 跨源资源共享(CORS) is a mechanism that uses additional HTTP头文件来讲述一个Browser Client to let the ǞǞǞ(测试中的应用)。运行在一个原点(域)的网络应用程序有权限访问来自不同原点的服务器的选定资源。一个网络应用程序使一个cross-origin HTTP request当它请求一个与它自己的原点不同的资源(domainprotocolport)时。

Example of an origin

下面是一个与URL进行产地比较的例子http://store.company.com/dir/page.html

URL                                                  Outcome    Reason
http://store.company.com/dir2/other.html             Success
http://store.company.com/dir/inner/another.html      Success
https://store.company.com/secure.html                Failure    Different protocol
http://store.company.com:81/dir/etc.html             Failure    Different port
http://news.company.com/dir/other.html               Failure    Different host

What went wrong

当你试图通过frames进行循环时,你的脚本/程序试图使用脚本访问一个具有不同来源的<iframe>,这将是一个巨大的安全漏洞如果你会实现它。如上所述同源政策浏览器会阻止试图访问不同来源的<iframe>的脚本。

两个页面具有相同的起源,如果protocol, port(如果指定了一个),以及host对两个网页来说都是一样的。你会看到这被称为"scheme/host/port tuple"有时(其中 "元组 "是由三个组成部分组成的一组,它们共同构成一个整体)。也许protocol, 领域, 主机名port must be the same of your same 领域 when you want to access the desired frame.

Solution

The ǞǞǞ可能包含许多框架 / i框架和some of them may be loaded only after certain 脚本 / 阿贾克斯他们中的一些人可能已经完成了学业。style属性设置为显示:无。 or visiblity作为hidden.当然不需要与所有的人互动。因此,这将是一个更好的方法来确定属性 of the <iframe>和switch accordingly. You can switch to an <iframe> through:

  • Frame Name
  • Frame ID
  • Frame Index
  • WebElement
  • 每个最佳实践当你打算切换到一个框架诱导时呼叫中心 for 帧可用并切换到它作为per the references below.

    在这里,你可以找到有关的讨论未被发现的DOME异常

    References

    Some references:

  • 在本讨论中,你将发现关于以下方面的详细分析SecurityError:阻止了一个有原点的框架访问一个跨原点的框架

  • 在这次讨论中,你会发现关于以下方面的不同方法在Selenium Webdriver Java中,是否可以不使用driver.switchTo().frame("frameName")来切换到一个框架中的一个元素?

  • 在本讨论的A Better Approach to Switch Frames部分中,你会发现有关的不同方法。如何在Selenium中选择一个html元素,无论它在哪个框架中?

  • cruisepandey
    cruisepandey
    发布于 2018-07-04
    0 人赞同

    你可以试试这样的方法:(不确定是否是JavaScript)。

    var iframeElems = driver.findElements(webdriver.By.tagName("iframe"));
    

    遍历这个列表以获得属性。

    for (var f = 0; f < iframeElems.length; f++) {
        console.log(iframeElems.getAttribute("attribute name"))
        
    Angia
    Angia
    发布于 2018-07-04
    0 人赞同

    Dirty solution:

    for windows:

    chrome.exe --user-data-dir="" --disable-web-security
    

    用于Mac。

    open -a Google\ Chrome --args --disable-web-security --user-data-dir=""
    

    通过这种方式,你打开chrome,让它忽略网络安全。

    PixelEinstein
    PixelEinstein
    发布于 2018-07-04
    0 人赞同

    你可以使用 selenium 来获取这样的iframe标签。

    var iframeElems = driver.findElements(webdriver.By.xpath("//iframe"));