How to find id of element clicked on screen?
See original GitHub issueI 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:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You can get the id in the browser context and send it:
window.processClick(event.target.id);It’s solved! The issue with my code at the beginning was that I was not getting the
event.target.idin 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!