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.

How to find id of element clicked on screen?

See original GitHub issue

I would like to click on elements and store their ids. I have the following code right now which is resulting in an empty event argument in the expose function. What’s going wrong?

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {
let browser = await puppeteer.launch({ headless: false });
let page = await browser.newPage();
// Expose a handler to the page
  await page.exposeFunction('onClick', (event) => {
    console.log(`Event fired: ${event}`);
});

// listen for events of type 'status' and
// pass 'type' and 'detail' attributes to our exposed function
await page.evaluateOnNewDocument(() => {
    window.addEventListener('click', (event) => {
        window.onClick(event);
    });
});

await page.goto("https://www.facebook.com/login");

})();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vsemozhetbytcommented, Oct 11, 2020

You can get the id in the browser context and send it: window.processClick(event.target.id);

0reactions
aizazansaricommented, Oct 12, 2020

It’s solved! The issue with my code at the beginning was that I was not getting the event.target.id in the browser context, like you mentioned. Furthermore, the empty values for event.target.id were a result of input fields not having id values; I used name values for them instead. Thanks for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

onClick to get the ID of the clicked button - JavaScript
First Way: Send trigger element using this ; button id="btn01" onClick="myFun(this)"> ; button id="btn02" onClick="myFun(this)"> ; button id="btn03" onClick="myFun ...
Read more >
How to Get the ID of the Clicked Element in the JavaScript ...
One way to let us get the ID of the element when we click on an element is to set the onclick property...
Read more >
How to get the ID of the clicked button using JavaScript/jQuery
Example 1: This example sets an onClick event by using click() method to each button, When button is clicked, the ID of the...
Read more >
How to Get the ID of a Clicked Element in JavaScript
To get the id attribute of a DOM element on click, create an event listener using AddEventListener() and retrieve the id of the...
Read more >
Get ID of Clicked Element using JavaScript
The JavaScript function will take the selected button element as object inside the variable named 'btn'. This variable will be used as object...
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