本文介绍了未捕获的 DOMException:阻止了一个源为“http://localhost:8080"的框架;在页面中列出 iframe 时访问跨源框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试列出页面中所有 iframe 的名称,以便我可以通过 Selenium 访问它们.

I am trying to list the names of all the iframes in a page, so I can access them through Selenium.

问题是 iframe 的名字每次都改变,所以我需要遍历所有的.

The problem is that the name of the iframe changes each time, so I need to loop through all of them.

我得到:

未捕获的 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 的名称?

Is there a way to get the name of the iframe in a different way?

推荐答案

此错误信息...

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

...暗示 WebDriver 实例阻止访问跨域框架.

...implies that the WebDriver instance blocked from accessing a cross-origin frame.

同源政策 :同源策略限制从一个源加载的文档或脚本如何与来自另一个源的资源交互.它是隔离潜在恶意文档的关键安全机制.

跨域资源共享 (CORS) :跨域资源共享 (CORS) 是一种机制,它使用额外的 HTTP 标头 来告诉 浏览器客户端AUT(应用程序在测试下) 运行在一个源(域)有权限从不同源的服务器访问选定的资源.当 Web 应用程序请求具有不同来源(domainprotocol端口)而不是它自己的来源.

Cross-Origin Resource Sharing (CORS) : Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell a Browser Client to let the AUT (Application under Test) running at one origin (domain) have permission to access selected resources from a server at a different origin. A web application makes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, and port) than its own origin.

以下是与 URL http://store.company.com/dir/page.html

Here is an example of origin comparisons to the 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

出了什么问题

当您尝试遍历 frames 时,您的脚本/程序尝试使用 JavaScript 访问具有不同来源的 <iframe>,这将是一个巨大的安全性缺陷,如果你能做到的话.如上所述,同源策略浏览器会阻止脚本尝试访问具有不同来源的 .


What went wrong

When you tried to loop through frames your script/program tried to access an <iframe> with different origin using JavaScript which would been a huge security flaw if you would have achieved it. As mentioned above the same-origin policy browsers block scripts trying to access a <iframe> with a different origin.

如果协议端口(如果指定了)和主机对于这两个页面都相同,则两个页面具有相同的来源网页.您有时会看到这被称为 scheme/host/port tuple"(其中元组"是三个组件的集合,它们共同构成一个整体).也许协议主机名端口在您要访问时必须与您的同一个域相同所需的帧.

Two pages have the same origin if the protocol, port (if one is specified), and host are the same for both the webpages. You'll see this referred to as the "scheme/host/port tuple" at times (where a "tuple" is a set of three components that together comprise a whole). Perhaps the protocol, domain, hostname and port must be the same of your same domain when you want to access the desired frame.

AUT 可能包含许多frames/iframes,其中一些可能仅在某些JavaScript 之后加载/Ajax 已经完成,因为其中一些可能将 style 属性设置为 display:none;visiblity隐藏.当然,不需要与所有人互动.因此,识别属性并相应地切换将是一个更好的方法.您可以通过以下方式切换到 :

The AUT may contain numerous frames / iframes and some of them may be loaded only after certain JavaScript / Ajax have completed where as some of them may be having style attribute set as display:none; or visiblity as hidden. Of-course won't require to interact with all of them. So it will be a better approach to identify the attributes of the <iframe> and switch accordingly. You can switch to an <iframe> through:

  • 帧名称
  • 帧 ID
  • 帧索引
  • WebElement

根据最佳实践,当您打算切换到框架时会导致 WebDriverWait 用于 frameToBeAvailableAndSwitchToIt 根据以下参考资料.

As per best practices when you intent to switch to a frame induce WebDriverWait for frameToBeAvailableAndSwitchToIt as per the references below.

在这里你可以找到关于未捕获的 DOMException

一些参考:

在本次讨论中,您将在 Selenium Webdriver Java 中是否可以在不使用 driver.switchTo().frame(frameName") 的情况下切换到框架中的元素?

In this discussion you will find the different approaches on Is it possible to switch to an element in a frame without using driver.switchTo().frame("frameName") in Selenium Webdriver Java?

在本讨论的 切换帧的更好方法 部分,您将在 如何选择 html 元素,无论它在什么框架中在硒中?

In the A Better Approach to Switch Frames section of this discussion you will find the different approaches on How can I select a html element no matter what frame it is in in selenium?

这篇关于未捕获的 DOMException:阻止了一个源为“http://localhost:8080"的框架;在页面中列出 iframe 时访问跨源框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:46