This message indicates that...
Uncaught DOMException: Blocked a frame from accessing a cross-origin frame originating from "http://localhost:8080".
The WebDriver instance is restricted from accessing a cross-origin frame.
Cross-Origin Security Policy
Same-origin policy : This policy limits how content from one origin can interact with resources from another origin, serving as a key security measure for preventing malicious interactions between documents.
Cross-Origin Resource Sharing (CORS)
Cross-Origin Resource Sharing (CORS) : Utilizing additional HTTP headers, CORS enables a Browser Client to grant permission for the Application under Test (AUT) on one origin to access specific resources from a server on a different origin through cross-origin HTTP requests.
Example of Origin Comparison
Consider these comparisons based on 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
Understanding the Issue
Your attempt to navigate through frames
involved accessing an <iframe>
with a distinct origin using JavaScript, posing a significant security risk. The same-origin policy enforced by browsers prevents scripts from interacting with frames of different origins.
For two pages to have the same origin, the protocol, port (if specified), and host must match. This concept is sometimes addressed as the "scheme/host/port tuple"
, requiring identical protocol, domain, hostname, and port for proper access within the same domain.
Solution
In a scenario where an AUT contains multiple frames / iframes, some may load after specific JavaScript or Ajax operations, while others may be hidden with properties like display:none; or visibility:hidden. Identifying attributes of the <iframe>
and switching accordingly is recommended. Switching to an <iframe>
can be achieved via:
Frame Name
Frame ID
Frame Index
WebElement
Following industry best practices, consider using WebDriverWait along with frameToBeAvailableAndSwitchToIt for efficient frame handling.
For further insights, refer to this discussion on Uncaught DOMException
References
Explore additional references:
A comprehensive breakdown on SecurityError: Blocked a frame with origin from accessing a cross-origin frame
Different approaches discussed in Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
Varied strategies outlined in How can I select a html element no matter what frame it is in in selenium?