Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Always reveal html element in console.log (Chromium)

I have a js script with a console.log that sometimes prints html elements.

Chrome has two modes of printing such DOM elements:

  • In html style like <div class="abc">...</div>, where hovering highlights the element in the page, and click opens the DOM subtree.
  • In object style like div.abc, where hovering does nothing, and click opens the object properties.

I always want the first format.

But for some reason, when printing a lot of these elements, Chrome randomly switches to the second format.

At first I thought this happens if there is not enough space, e.g. if we print other values in the same console.log call. But this seems not to be the case.

Any idea how I can always get the first format? Either a setting in Chrome/Chromium, or something I can do in my script.

EDIT

I will try to come up with a reproducible example. It will be difficult because this happened on a website with already a lot going on.

For now I will simply mention that by "a lot of these elements", I mean repeated calling of console.log(element) with different elements. The first 100 or so calls would give me the first representation, then it would switch to the other representation. But this was not at all consistent, and there was no specific event that would explain such a switch.

New data points since initial version of the question:

  • console.dirxml(element) also switches to the JSON prepresentation (= second format), at the same time that console.log does.
  • console.log(element, element instanceof HTMLElement) always prints [element], true, no matter whether the json or xml representation is used to print the element.
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You cannot get always this format because that is unpredictable in the blink engine, and the source code is the proof, moreover there is a comment exactly for this case:

function printNode(nodeId)
{
    if (!nodeId) {
        // Sometimes DOM is loaded after the sync message is being formatted, so we get no
        // nodeId here. So we fall back to object formatting here.
        this._formatParameterAsObject(object, elem, false);
        return;
    }
about 4 years ago · Santiago Trujillo Report

0

for some reason, when printing a lot of these elements, Chrome randomly switches to the second format

Without a minimal, reproducible example demonstrating both cases, it's hard to provide a fully-satisfying answer, but according to the documentation, the following methods will print the XML representation of a node:

  • console.dirxml(node)
  • console.log(node)

While, in contrast, console.dir(node) will print a JSON representation of the node.

There is also a console utility function for navigating (programmatically focusing) a node/object: inspect(object/function).


If, by "printing a lot of these elements" you just mean an object collection like an Array or NodeList, then that's not a level of display control that Chromium's dev tools offers you. Instead, you should iterate each node and print it (or provide each node as an argument to console.log, for example:

const divArray = [...document.querySelectorAll('div')];
console.log(divArray); // JSON representation of an array (of div elements)
console.log(...divArray); // one XML representation for each div element
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!