2

I filtered the HTML class elements using

elements = document.querySelectorAll("div._3arMG")

Now that I have a list of HTML elements I need, I want the CSS Selector/Path for each HTML element in the elements list.

CSS Selector when I right click on HTML ELement in Console -> Copy -> Copy Selector -> #root > div._3arMG

Please suggest how do I get this CSS Selector using Javascript?

2 Answers 2

2

You could use either Element.querySelector() or Element.querySelectorAll(), choose which suits your case

const elements = document.querySelectorAll("div._3arMG")

for (const element of elements){
  const subElements = element.querySelectorAll('.someclass')
  // or
  const subElement = element.querySelector('.someclass')
  
  // ... 
}

Reference

Element.querySelector()

Element.querySelectorAll()

Sign up to request clarification or add additional context in comments.

3 Comments

by .someClass , do you mean unique id/class for each HTML element in elements?
@SaiAvinashDuddupudi it queries all descendant elements which the selectors your defined, .someclass here is just example. If each descendant element has their own id, you could not need to query like this, in that case just use ` document.getElementById()` though. Correct me if I misunderstand what you said
Element.queryseelctor() doesnt return seelctor. did not get what you said
0

You can do it recursivly with parentElement for the next node above. Getting for every node the tagname by nodeName, the classes with classList and the id.

Edited: Because Id's are unique I stop if an id occurs in the path.

let elements = document.querySelectorAll("div._3arMG");

let result = [];
elements.forEach(el => {
    result.push(getPath(el));
})
console.log(result);

function getPath(el) {
    let act = el.nodeName;
    el.classList.forEach(cl => act += '.' + cl);
    if (el.id) act += '#' + el.id;
        
    if (!el.id && el.parentElement) {
        let res = getPath(el.parentElement);
        res.push(act);
        return res;
    } else {
        return [act];
    }
}
<div class='Wrapper'>
  <div class="_3arMG myClass">
    Test
  </div>
  <table id='myTable'>
    <tr>
      <td>
        <div class="_3arMG">My span</div>
      </td>
    </tr>
  </table>
</div>

4 Comments

As IDs should be unique, you could (possibly) stop when it hits an element with an id.
@freedomn-m - I suggested this too, but the OP shows us -> Copy -> Copy Selector -> #root > div._3arMG where the id rootis in path so I build it complete.
"Copy -> Copy Selector" are the actions made in the browser :) (not saying it's wrong to include the entire tree - bit hard to see what OP wants and why)
@freedomn-m - I changed itnow, after I see that this is really not part of the path ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.