So I am using an extension which should open a page and click a button. So it just opens the page but doesn't click the button. What am I doing wrong? The ID of the button is right. I tried to put it in the console and it clicked the button.
// Import On/Off Toggle window.onload = function () { document.getElementById("onofftoggle").onclick = function onoff() { // Get the checkbox var checkBox = document.getElementById("onofftoggle"); // If the checkbox is checked, display the output text if (checkBox.checked == true) { text.style.display = "block"; // Run the main script //Get Wikipediabutton with ID "run" document.getElementById('run').addEventListener('click', onclick, false) // Call open window function function sleep(milliseconds) { return new Promise(resolve => setTimeout(resolve, milliseconds)); } async function onclick() { window.open('https://www.mediamarkt.de/de/product/_trust-gxt-323-carus-2724281.html'); await sleep(5000); var hangoutButton = document.getElementById("pdp-add-to-cart-button"); hangoutButton.click(); // this will trigger the click event } } else { text.style.display = "none"; } } }
documentindocument.getElementById("pdp-add-to-cart-button");is the current window's document, not the window you opened. Set a variable to the output ofwindow.openand usewin.document.getElementById(...).