Using the Chrome Dev Tools console, I'm trying to select an element inside an iframe on the page. Is there a way to do this programmatically without having to select the frame in the frames dropdown in order to set the console context to that frame first? Assuming the target iframe is frames[1], and the element inside that iframe has an id of "some-elem", the following does not seem to work:
frames[1].document.getElementById('some-elem');
I think you want contentDocument instead of document (see this related question).
Note that this will only work if iframe and main document are in the same domain. Otherwise, you are attempting cross-site scripting and it will be blocked by the browser.
Related
Similar to the posts below:
Set focus on iframe in Chrome
Setting focus to iframe contents
From the root/parent I want to be able to pass focus to the iframe.
Anything that may be in focus in the iframe is fine.
iframe.contentWindow.focus();
However, the iframe.contentWindow is not accessible because its a cross-origin frame.
Seems like the answer is: not possible.
If there is a script running in the iframe you can directly call focus from there.
I am trying to develop a web application which has functionality similar to the Element Selector in developer tools in Chrome.
I need to open any URL from other different domains, inside a container in my app, and the page should appear exactly as it appears in normal browser, with all it styles. Then I should be able to hover over the elements in the opened page, highlight it, and display some property information about this element.
I created a simple Angular app and used Iframe tag to open the third party page. But, when I try to attach event handlers, with addEventListener, to the DOM elements in the Iframe, I get the cross-domain error message.
Uncaught DOMException: Blocked a frame with origin
"http://localhost:4200" from accessing a cross-origin frame.
I read some posts in stackoverflow like “Cross site contents cannot be read by JavaScript” and “If you don't have control over the framed site, you cannot circumvent the cross-domain policy.” For more details check the links:
Get DOM content of cross-domain iframe
Cross domain iframe issue
Following is my code:
<iframe id="pFrame" onload="frameLoad();" src="" />
function frameLoad() {
let frame = document.getElementById("pFrame");
let iframeWindow = frame.contentWindow;
iframeWindow.addEventListener("mouseover", mouseOver, true);
iframeWindow.addEventListener("mouseout", mouseOut, true);
iframeWindow.addEventListener("click", onClick, true);
}
Do you have any advice or maybe suggest different technique to solve this problem?
Thanks
Have you tried looking the Mozilla Goggles project ?
It has an integrated editor, and their inspector seems to be very powerful.
Plus it don't require any application on your computer.
I am trying to get elements by the Chrome developer console using the function document.querySelectorAll, the point is that it does not return any element, however I see the elements on the Elements tabs.
I was wondering whether someone has faced similar issues. Shall I change some options on the browser configuration?
By the way, the Chrome version is 63 on MAC. In addition, the page I am working on has an iframe html tag, may this be the reason of the strange behavior?
This is what I get from the Developer Console
And this is what I get from the elements tabs:
There aren't any browser settings that would affect document.querySelectorAll(). It's pretty core functionality.
You mentioned an iframe, so it's likely that is the source of the confusion. When using iframes, you can't access or modify the contents of the iframe directly from the outer level. To the outer level, it's essentially a black box. This is due to sandboxing that the browser does.
The exception to this is if the iframe and the main page are on the same domain (e.g., http://example.com/page1 and http://example.com/page2).
If they are both on the same domain, then you can access it's window with contentWindow:
const iframe = document.querySelector('iframe');
iframe.contentWindow // the window for the iframe
From there, you can access its document, and run querySelectorAll() against that:
iframe.contentWindow.document.querySelectorAll('div');
That will get all of the div elements in the iframe.
I have developed an add-on for Firefox and Chrome. It has content scripts. I want to access them in the browser tab's console (on Firefox the Web Console). For example, I want to enter a global variable defined in the content script(s) in the console, and it will output its value.
In Chrome, I can open the console by pressing F12, then navigate to the Console tab in the developer tools. It has a dropbox, right after the filter button, to select which context I am in (page/content script):
In Firefox, how to do the same thing?
The ability to change the context/scope of the Web Console (opened directly with Ctrl-Shift-K or F12 and selecting the Console tab) to that of the content scripts for an extension does not appear to exist. In addition, this capability does not exist in any of the other ways to view a console in Firefox. A bug/RFE should be filed on Bugzilla requesting this functionality; it would be quite useful. You will want the RFE to clearly explain that there should be the ability to switch to the content script context/scope for each frame in the tab (i.e. the top frame and each child frame). This should be the case for both the Console and the Debugger.
NOTE: You can change the console into the context/scope of the iframe's page scripts by selecting the frame from the drop-down menu opened from the frame-selector drop-down:
If this drop-down icon is not appearing for you, go to the DevTools settings and check "Select an iframe as the currently targeted document". However, doing this A) does not switch into the content script context/scope and B) does not work properly with the Web Debugger (testing in the current version of Firefox and Nightly (54.0a1).
Web Debugger
You can use the Web Debugger (Ctrl-Shift-S, or F12 and selecting the Debugger tab) with WebExtension content scripts. The content scripts for the extension are listed in the "Sources" under a moz-extension:// URL. You will need to identify the UUID that is used for the extension. You can view the content of variables, set breakpoints, etc. However, this does not give you the ability to explicitly switch to the context of the child frame. Placing debugger; directives in the JavaScript which is running in the child iframe is ineffective.
Web Debugger debugging content script (in top frame):
Console in background script context
If you were wanting to open a console which was in the context of your WebExtensions' background script, you could do so by clicking on the Debug button for your extension in about:debugging. However, this will not get you access to a console in the content script's context.
Workarounds for seeing variable values in the iframe
For what you need: unambiguously indicating that values are in the iframe context, not the top frame; I see two methods of doing so:
Use console.log() with information prepended that clearly indicates that the script believes it is running in the iframe. For example:
console.log('In iframe', 'foo=', foo);
So that you don't have to have 'In iframe' in every call to console.log() you make, you could create a function that prepends that text to all calls to that function. You could even override the console.log() function so your code still just calls console.log().
However, this only tells you that your code thinks that it is running in the iframe. Part of what you may be debugging is your content script code detecting that it is in an iframe.
This method does not indicate with certainty that the reported values are actually in the iframe.
Store values into the DOM using Element.dataset, or other DOM element attributes, and then inspect the DOM for these values. To view these attributes, I find that the DOM Inspector shows these quite clearly:
This method can be used to unambiguously show that the values are ones in the iframe, and exactly which iframe, without relying on the code running in the iframe to accurately determine that it is in an iframe and which iframe it is in.
A simple solution is to just console.log() in the content script and then click the sourcemap link to view the script. As shown below:
It's not yet possible. There is a bug Implement UI for switching context to content script opened (since Nov 2017) for that.
In Firefox Developer Edition, go on "about:debugging" page and click on the "Debug" button beside your add-on to open the dev tools.
In any YouTube video page (Gangnam Style for example), some elements can't be accessed by their XPath. From example, I'm trying to access the "Show more" button
by getting its XPath from the Inspect Window
and using this code
btn=$x('//*[#id="widget_bounds"]/div[2]/div[4]/div[7]/div[3]/span[1]');
but I get nothing, or more precisely, an empty list:
I've never encountered this problem before, are they using some sort of obfuscation trickery to prevent the item from being accessed?
Is there a way to work around it?
The content you want to reach is in an iframe on the page. BUT the problem is you will not be able to get to the content because the Same Origin Policy is going to prevent it.
document.getElementById("I0_1392927253257").contentWindow.document
SecurityError: Blocked a frame with origin "http://www.youtube.com" from accessing a cross-origin frame.
Check out #sashoalm's link below to change the context of iframe in Chrome.