question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Can I watch for changes in a specific element?

See original GitHub issue

Hi!

I’m trying watch for changes in a certain div that keeps getting new elements added to it. I couldn’t seem to find a proper way of listening to such “change” event via the API, am I missing something?

Thanks in advance.

P.S: I though about using MutationObserver but page.$$('.someClass') isn’t returning a Node type object (not to mention the workaround of using window in a nodejs environment just to be able to use MutationObserver).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
dgozmancommented, Oct 5, 2020

There is no such API. Using MutationObserver is a good way to achieve this. Note that you have to evaluate the whole script in the page context:

// Get a handle to the div.
const divHandle = await page.$('text=Target div');
await divHandle.evaluate(div => {
  new MutationObserver((mutationsList, observer) => {
    // Handle mutations.
  }).observe(div, { childList: true, subtree: true });
});

Does this answer your question?

3reactions
Ron-SSGcommented, Oct 5, 2020

Thanks a lot for all the help!

I ended up using page.exposeFunction() to allow me to use the Node.js API like so:


const myClassHandle = await page.$('.myClass');

page.exposeFunction('myNodeFunc', async () => {
	externalNodeFunc(myClassHandle);
});

await myClassHandle.evaluate((div) => {
  return new Promise(resolve => {
    new window.MutationObserver((mutationsList, observer) => {
      if (div.childElementCount >= 1) {
        myNodeFunc();
        resolve();
      }
    }).observe(div, { childList: true });
  });
});

// Find the latest element added to the div and get its internals
const externalNodeFunc = (myClassHandle) => {
  const whatIWantedToFind = await myClassHandle.$$('.mySubClass');
  const lastChildAdded = await whatIWantedToFind[ whatIWantedToFind.length -1 ].$$('.inner-element');
}

Hope this could help someone in the future 😄 Thanks again @dgozman !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Watching for when any element changes
This contains a set of elements that should, when they change (doesn't matter which element changes) - should (for example) be stored in...
Read more >
Elements and atoms (video) | Khan Academy
So the neutrons can change, the electrons can change, you can still have the same element. The protons can 't change. You change...
Read more >
Matrix Table Question
If you have set your matrix table to the likert format, you can change the answer format. Allow one answer: Respondents can choose...
Read more >
COVID-19 Guidance for Hospital Reporting and FAQs For ...
[CHANGE] Hospitals that encounter reporting challenges or have questions should contact the NHSN helpdesk (nhsn@cdc.gov). Data Elements. The following data ...
Read more >
Debugging CSS - Learn web development | MDN
While you may choose to mostly develop in a particular browser, ... and values applied to elements on your page, and making changes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found