Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookies not loading in discord selenium python

I have such a situation, I log into the discord and then save the cookies with this code

pickle.dump (driver.get_cookies (), open (r "cookies_discord.pkl", "wb"))

after which I switch to a new browser and try to load cookies on the https://discord.com/ discord page on it and save cookies

but after the cookies are loaded, the page is still not logged in to me

with twitter all the same works fine

here is the code for loading cookies

for cookie_discord in pickle.load (open (r "cookies_discord.pkl", "rb")): driver.add_cookie (cookie_discord)

like image 363
YoungFire Avatar asked Jul 02 '26 13:07

YoungFire


1 Answers

The browser version of Discord doesn't just use cookies for login.

However, you can get access to your account using your user token, which is a pretty long string that looks something like this:

"A1b2C3d4E5f6.G7H8.i9J0A1b2C3d4E5f6"

With this string, you have absolute access to the account, and it is valid until you change your password. (No password, authenticator, or 2FA required)

You can get your user token by running this JavaScript in your browser console:

(webpackChunkdiscord_app.push([[''],{},e=>{m=[];for(let c in e.c)m.push(e.c[c])}]),m).find(m=>m?.exports?.default?.getToken!==void 0).exports.default.getToken()

Once you have your user token, you can login to your account using this JavaScript (change the "PASTE_YOUR_TOKEN_HERE" with your own token):

(function() {window.t = 
"PASTE_YOUR_TOKEN_HERE"
;window.localStorage = document.body.appendChild(document.createElement `iframe`).contentWindow.localStorage;window.setInterval(() => window.localStorage.token = `"${window.t}"`); window.location.reload();})();

I advise you run this JavaScript every time you run your bot, instead of trying to achieve it using cookies.

You can run JS scripts in Python Selenium using this function:

driver.execute_script("JavaScript code here")
like image 183
Wutong Avatar answered Jul 04 '26 02:07

Wutong