'Javascript snippet in dev tools seems to not execute after a web page changes url

I'm very new to JavaScript and trying to automate a small test process in a browser (Sources - Snippets) with JavaScript, and it spans over several web pages, and I noticed that after a page reloads or a new page loads the subsequent code seems to not execute. Even if I set a big timeout. The code lines executed separately in the "console" tab of the dev tools work fine. So the question is: what shall I do to make them execute one after another in one snippet?

Here is just one step after which nothing seems to happen:

(function() {
  //Link   
  document.querySelector("#main-content > p:nth-child(6) > a").click(); // a click works, but the next line does not do anything even with a big timeout.

  //LOGIN FORM
  //User name
  setTimeout(() => {
    document.querySelector("form input[formcontrolname='benutzername']").value = 'xxxx'
  }, 3000); //when executed separately in the console, it works.
})()


Solution 1:[1]

Got an answer from an expert: "When automating from the console, the code won't survive a page reload because the code is running in the document process, so when it changes the code is gone."

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 eifcgn24