Con*_*ony 1 python selenium webdriver selenium-webdriver firefox-profile
我正在尝试使用 selenium 读取来自https://web.telegram.org的电报消息。
当我在 firefox 中打开https://web.telegram.org时,我已经登录,但是当从 selenium webdriver(firefox) 打开同一页面时,我得到登录页面。
我看到 telegram web 不使用 cookie 进行身份验证,而是将值保存在本地存储中。我可以使用selenium访问本地存储并在那里拥有密钥,例如:“dc2_auth_key”,“dc2_server_salt”,“dc4_auth_key”,...但我不知道如何处理它们才能登录(如果我这样做需要对它们做一些事情,那么为什么?它是同一个浏览器,为什么在没有硒的情况下打开时它不能正常工作?)
重现:
打开 Firefox 并登录https://web.telegram.org然后运行以下代码:
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()
Run Code Online (Sandbox Code Playgroud)
当您https://web.telegram.org使用Firefox手动打开时,将使用默认的 Firefox 配置文件。当您登录并浏览网站时,网站会在您的系统中存储身份验证 Cookie。由于 cookie 存储在默认 Firefox 配置文件的本地存储中,即使重新打开浏览器,您也会自动进行身份验证。
但是,当每次启动Firefox时创建一个临时的新mozprofile时, GeckoDriver都会为您的测试启动一个新的 Web 浏览会话,这从以下日志中可以明显看出:
\n\nmozrunner::runner INFO Running command: "C:\\\\Program Files\\\\Mozilla Firefox\\\\firefox.exe" "-marionette" "-profile" "C:\\\\Users\\\\ATECHM~1\\\\AppData\\\\Local\\\\Temp\\\\rust_mozprofile.fDJt0BIqNu0n"\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n您可以在Is it Firefox or Geckodriver中找到详细讨论,它会创建 \xe2\x80\x9crust_mozprofile\xe2\x80\x9d 目录
\n
一旦测试执行完成并被quit()调用,临时mozprofile将在以下过程中删除:
webdriver::server DEBUG -> DELETE /session/f84dbafc-4166-4a08-afd3-79b98bad1470 \ngeckodriver::marionette TRACE -> 37:[0,3,"quit",{"flags":["eForceQuit"]}]\nMarionette TRACE 0 -> [0,3,"quit",{"flags":["eForceQuit"]}]\nMarionette DEBUG New connections will no longer be accepted\nMarionette TRACE 0 <- [1,3,null,{"cause":"shutdown"}]\ngeckodriver::marionette TRACE <- [1,3,null,{"cause":"shutdown"}]\nwebdriver::server DEBUG Deleting session\ngeckodriver::marionette DEBUG Stopping browser process\nRun Code Online (Sandbox Code Playgroud)\n\n因此,当您使用Selenium、GeckoDriver和Firefox打开同一页面时,存储在默认 Firefox 配置文件的本地存储中的 cookie不可访问,因此您将被重定向到登录页面。
\n\n要在本地存储中存储并使用 cookie来自动进行身份验证,您需要创建并使用自定义 Firefox 配置文件。
\n\n\n\n在这里您可以找到有关 webdriver.FirefoxProfile() 的相关讨论:Is it possible to use a profile without made a copy of it?
\n
| 归档时间: |
|
| 查看次数: |
4544 次 |
| 最近记录: |