未捕获的DOMException:阻止具有原点“http:// localhost:8080”的帧。从页面中列出iframe时访问跨域框架 [英] Uncaught DOMException: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame while listing the iframes in page

查看:1175
本文介绍了未捕获的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应用程序请求具有不同来源的资源( domain 协议)时,它会生成跨源HTTP请求 port )而不是它自己的来源。

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






出了什么问题



当你试图循环thr ough frames 您的脚本/程序尝试使用JavaScript访问不同来源的< iframe> 安全漏洞如果您已经实现了它。如上所述,同源策略浏览器阻止尝试访问具有不同来源的< 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 (其中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 可能包含多个 / iframes ,其中一些只有在某些 JavaScript / Ajax 完成之后才能加载,因为其中一些可能有 style 属性设置为 display:none; visiblity 隐藏。粗糙的不需要与所有这些进行交互。因此,识别< iframe> 属性并进行相应切换是一种更好的方法。您可以通过以下方式切换到< iframe>

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-coarse 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

  • Frame Name
  • Frame ID
  • Frame Index
  • WebElement

根据最佳做法,当您打算切换到框架时,会导致 WebDriverWait 。 io / selenium / docs / api / java / org / openqa / selenium / support / ui / ExpectedConditions.html#frameToBeAvailableAndSwitchToIt-org.openqa.selenium.By-rel =noreferrer> frameToBeAvailableAndSwitchToIt 根据以下参考资料。

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

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

Here you can find a relevant discussion on Uncaught 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元素,无论它在selenium中的哪个帧?

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时访问跨域框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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