Get HTML element via aria label

后端 未结 3 967
不思量自难忘°
不思量自难忘° 2021-02-07 02:48

I\'m making a small chrome extension and for it I need to grab a div from the DOM to manipulate. I get the DOM but I\'m having trouble grabbing the required d

3条回答
  •  臣服心动
    2021-02-07 03:16

    Probably you're trying to get the Element before it's even loaded on the page.

    If so, you can wrap your code into the DOMContentLoaded event:

    document.addEventListener('DOMContentLoaded', function() {
      console.log(document.getElementById(':ik').textContent);
    });
    

    But if you really want to get the element by its aria label, you can do that:

    document.querySelector('div[aria-label="Message Body"]');
    

    But this way is much less performatic, and you'll need to do exactly what I've mentioned above too.

提交回复
热议问题