'JavaScript waiting for load page and click

I need to type movie title in the search input and click on the "Search" button, then I need to wait for the page load and click on it I need to do it in Console of Browser Here is my code

document.getElementById('story').value='Troy';
document.querySelector('body > div.wrapper > div.header > div.header44 > div.search_panel > span > form > button').click();
document.addEventListener("DOMContentLoaded", () => {
document.getElementsByTagName('a')[1].click();
} );

If I do these steps separately, everything works. But I need to do it all at once (by one script) Here is the last action, waiting for the page load and clicking on it

document.addEventListener("DOMContentLoaded", () => {
document.getElementsByTagName('a')[1].click();
} );

I think this last action is triggered ahead of time how to write this code correctly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source