0

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"; } } } 
5
  • document in document.getElementById("pdp-add-to-cart-button"); is the current window's document, not the window you opened. Set a variable to the output of window.open and use win.document.getElementById(...). Commented Jun 29, 2021 at 16:45
  • Does this answer your question? Javascript get element from opened window Commented Jun 29, 2021 at 16:48
  • Note that you may have issues with getting the button in the newly opened window, depending on the extension's capabilities. Commented Jun 29, 2021 at 16:49
  • Hey @YvesKlaus - following up. Was your question answered satisfactorily? If there is more we can help with, please add a comment below any answer, or edit your question to clarify what else you want to know. Otherwise, please choose a "best answer" (by clicking the checkmark beside the answer) to close out the question. If no answer provided helpful information, please add your own answer and select that as the best answer (to close the question). Remember, too: you can upvote any answers you found helpful (you can also upvote and checkmark the same answer, if desired.) Thanks! Commented Jul 30, 2021 at 19:38
  • @YvesKlaus This question is still open. Please choose a "best answer" (by clicking the checkmark beside an answer) to close out the question. If no answer provided helpful information, please add your own answer and select that as the best answer (to close the question). That would help us out. Thanks! Commented Nov 15, 2021 at 23:33

1 Answer 1

0

I assume you are using an extension like TamperMonkey (you should specify which extension for more finely-tuned assistance).

But if you are using an extension like TamperMonkey, sometimes you need to stick your code inside a setTimeout(), because the DOM just isn't quite ready at the moment the injected javascript is fired.

Over the years I've tried using various methods in my scripts like:

document.addEventListener('DOMContentLoaded', function() {}) 

and checking the document.readyState

-- and often those work --

But sometimes wrapping your code inside a setTimeout, as inelegant as that is, remains the best solution. My own approach is to start with a ridiculous timeout (say 10 seconds) and adjust down ( /2 ) until it fails.

setTimeout(function(){ //your code here },10000); //10sec timeout before your code runs 

Update:

However, looking again at your code, I see that you are trying to press the button after opening another window. Note that you will need to write a separate script and attach it to that new window (in TamperMonkey, via the //match line) because once that new window is opened your script, running on the first window, cannot see the button in the second window.

Sign up to request clarification or add additional context in comments.

1 Comment

The OP is already using a sleep function of 5 seconds between window.open and document.getElementById. They're just using the wrong document. A common mistake I'm sure we have plenty of duplicates for...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.