How to get an element into a variable in the dev console
23:39 25 Nov 2025

What's the easiest way to put an HTML element into a variable in the browser dev console?

I usually use this pattern:

  1. inspect the element, right click > copy selector path
  2. open the console, foo = document.querySelector('cmdv')

But this doesn't work when the selector path has escaping, e.g.

#root > div > div.hidden.sm\:block.w-\[250px\] > div > div > div

JavaScript doesn't have literal strings, and I don't know how to double-escape backslashes when I paste a string into the console.

javascript safari