1

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Cony
  • 213
  • 1
  • 8
  • 21
  • Selenium will open a new session, you would have to login into the website using selenium. wouldn't make any difference tho. – cruisepandey Jun 04 '19 at 12:25
  • @cruisepandey when i completely close firefox and open it again, then go to telegram web im still authenticated, and that is also a new session. so there must be another difference. – Cony Jun 04 '19 at 12:29
  • Possible duplicate of [Selenium use of firefox profile](https://stackoverflow.com/questions/37247336/selenium-use-of-firefox-profile) – JeffC Jun 04 '19 at 18:21

3 Answers3

7

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    Thank you so much for the detailed answer! copied my profile and point the code there and it works :) B.T.W how did you get these logs? when i run my code i get no logs from the webdriver.. – Cony Jun 04 '19 at 14:10
  • 1
    You need to enable the trace level logs to see these kind of logs. – undetected Selenium Jun 04 '19 at 14:11
0

You can auth using your current data from local storage. Example:

    driver.get(TELEGRAM_WEB_URL);
    LocalStorage localStorage =  ((ChromeDriver) DRIVER).getLocalStorage();
    localStorage.clear();
    localStorage.setItem("dc2_auth_key","<YOUR AUTH KEY>");
    localStorage.setItem("user_auth","<YOUR USER INFO>");
    driver.get(TELEGRAM_WEB_URL);
0

you can use this code to extract credential tokens from the browser's local storage, after logging in:

def get_credential_tokens(driver):
    tokens = driver.execute_script(
        "var tokens = {};\
        for (var i = 0; i < localStorage.length; i++){\
            tokens[localStorage.key(i)] = localStorage.getItem(localStorage.key(i));\
            };\
        return tokens;"
    )

    return tokens

you can store it in a JSON file or your .env file and read it from there and set it in the browser's local storage as follow:

credentials = load_credentials()

for key, value in credentials.items():
    set_item(key, value)

where the set_item() function is:

def set_item(driver, key, value):
    driver.execute_script("window.localStorage.setItem(arguments[0], arguments[1]);", key, value)

and the load_credentials() function is:

def __load_credentials() -> Dict:
    with open("tokens.json", "r") as f:
        credentials = json.load(f)

    return credentials

you can also save the credentials in a JSON file this way:

def save_credentials(credentials) -> None:
    with open("tokens.json", "w") as f:
        json.dump(credentials, f)