简体   繁体   中英

how to automatically run a javascript in console after redirecting to the next page

i'm trying to automate a login and logout script using javascript, but whenever it logs in, the console code clears up. is there a way to make it run thesame script after login? so that it will be able to logout automatically?

this script logs in but after login, it clears the console so it doesn't see the logout code:

var login= document.querySelector("button[type='submit']"); login.click();

var logout = document.querySelector("a[href='https://example.com/logout.php']"); logout.click();

console always refresh when you move page. so i think it's impossible to do it. if you want to make auto login system. maybe selenium or electron are the best way to make it.

here is the link that can help.

#selenium use python : https://www.selenium.dev/

#electron use nodejs : https://www.electronjs.org/

I know you wanted to run function on next page, but, anyways, here's what you could do if you stay on the same page:

 function login() { document.getElementById("bye").style.display="none"; document.getElementById("login").style.display="none"; document.getElementById("content").style.display=""; document.getElementById("logout").style.display=""; } function logout() { document.getElementById("bye").style.display=""; document.getElementById("login").style.display=""; document.getElementById("content").style.display="none"; document.getElementById("logout").style.display="none"; }
 <div id="bye" style="display:none">Bye!</div> <div id="login"><button id="loginBtn" onclick="login()">Login</button></div> <div id="content" style="display:none">content</div> <div id="logout" style="display:none"><button id="logoutBtn" onclick="logout()">Logout</button></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM