如何使用 selenium webdriver 将现有的登录令牌用于电报网络 [英] How to use existing login token for telegram web using selenium webdriver

查看:26
本文介绍了如何使用 selenium webdriver 将现有的登录令牌用于电报网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 selenium 读取来自 https://web.telegram.org 的电报消息.

当我在 Firefox 中打开 https://web.telegram.org 时,我已经登录了,但是当从 selenium webdriver(firefox) 打开同一页面时,我得到了登录页面.

我看到电报网络没有使用 cookie 进行身份验证,而是将值保存在本地存储中.我可以使用 selenium 访问本地存储并在那里有密钥,例如:dc2_auth_key"、dc2_server_salt"、dc4_auth_key",...但我不确定如何处理它们以登录(如果我这样做了)需要对它们做些什么那为什么?它是同一个浏览器,为什么在没有 selenium 的情况下打开时它的工作方式不一样?)

重现:

打开 firefox 并登录到 https://web.telegram.org 然后运行以下代码:

from selenium import webdriver驱动程序 = webdriver.Firefox()driver.get("https://web.telegram.org")# 我的代码在这里但无关紧要,因为我在登录页面.驱动程序关闭()

解决方案

当您使用 Firefox 手动打开 https://web.telegram.org 时,使用默认 Firefox 配置文件.当您登录并浏览网站时,这些网站会在您的系统中存储身份验证 Cookie.由于 cookie 存储在 默认 Firefox 配置文件本地存储中,即使重新打开浏览器,您也会自动进行身份验证.

但是当 GeckoDriver 每次在启动 Firefox 时创建一个临时的新 mozprofile 时,都会为您的测试启动一个新的网络浏览会话,这可以从以下日志:

mozrunner::runner INFO 运行命令:"C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.fDJt0BIqNu0n"

<块引用>

您可以在 是 Firefox 还是 Geckodriver,它创建了rust_mozprofile"目录

一旦测试执行完成并且quit()被调用,临时mozprofile在以下过程中被删除:

webdriver::server DEBUG ->删除/session/f84dbafc-4166-4a08-afd3-79b98bad1470geckodriver::marionette TRACE ->37:[0,3,"退出",{"flags":["eForceQuit"]}]牵线木偶跟踪 0 ->[0,3,"退出",{"flags":["eForceQuit"]}]Marionette DEBUG 不再接受新连接Marionette TRACE 0 <- [1,3,null,{"cause":"shutdown"}]geckodriver::marionette TRACE <- [1,3,null,{"cause":"shutdown"}]webdriver::server DEBUG 删除会话geckodriver::marionette DEBUG 停止浏览器进程

因此,当您使用 SeleniumGeckoDriverFirefox 存储在 默认 Firefox 配置文件本地存储中的 cookie 是无法访问,因此您将被重定向到登录页面.

<小时>

要在本地存储中存储和使用 cookie 以自动进行身份验证,您需要创建和使用自定义 Firefox 配置文件.

<块引用>

在这里您可以找到关于 webdriver.FirefoxProfile():是否可以使用配置文件而不复制它?

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()

解决方案

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?

这篇关于如何使用 selenium webdriver 将现有的登录令牌用于电报网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆