I’m trying to read telegram messages from https://web.telegram.org with selenium.
When i open https://web.telegram.org in firefox i’m already logged in, but when opening the same page from selenium webdriver(firefox) i get the login page.
I saw that telegram web is not using cookies for the auth but rather saves values in local storage. I can access the local storage with selenium and have keys there such as: “dc2_auth_key”, “dc2_server_salt”, “dc4_auth_key”, … but I’m not sure what to do with them in order to login(and if i do need to do something with them then why? its the same browser why wont it work the same when opening without selenium?)
To reproduce:
open firefox and login to https://web.telegram.org then run this code:
from selenium import webdriver driver = webdriver.Firefox() driver.get("https://web.telegram.org") # my code is here but is irrelevant since im at the login page. driver.close()
Advertisement
Answer
When you open https://web.telegram.org
manually using Firefox, the Default Firefox Profile is used. As you login and browse through the website, the websites stores Authentication Cookies within your system. As the cookies gets stored within the local storage of the Default Firefox Profile even on reopening the browsers you are automatically authenticated.
But when GeckoDriver initiates a new web browsing session for your tests everytime a temporary new mozprofile is created while launching Firefox which is evident from the following log:
mozrunner::runner INFO Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette" "-profile" "C:\Users\ATECHM~1\AppData\Local\Temp\rust_mozprofile.fDJt0BIqNu0n"
You can find a detailed discussion in Is it Firefox or Geckodriver, which creates “rust_mozprofile” directory
Once the Test Execution completes and quit()
is invoked the temporary mozprofile is deleted in the following process:
webdriver::server DEBUG -> DELETE /session/f84dbafc-4166-4a08-afd3-79b98bad1470 geckodriver::marionette TRACE -> 37:[0,3,"quit",{"flags":["eForceQuit"]}] Marionette TRACE 0 -> [0,3,"quit",{"flags":["eForceQuit"]}] Marionette DEBUG New connections will no longer be accepted Marionette TRACE 0 <- [1,3,null,{"cause":"shutdown"}] geckodriver::marionette TRACE <- [1,3,null,{"cause":"shutdown"}] webdriver::server DEBUG Deleting session geckodriver::marionette DEBUG Stopping browser process
So, when you open the same page using Selenium, GeckoDriver and Firefox the cookies which were stored within the local storage of the Default Firefox Profile aren’t accessible and hence you are redirected to the Login Page.
To store and use the cookies within the local storage to get authenticated automatically you need to create and use a Custom Firefox Profile.
Here you can find a relevant discussion on webdriver.FirefoxProfile(): Is it possible to use a profile without making a copy of it?