当您使用Firefoxhttps://web.telegram.org手动打开时,会使用默认 Firefox 配置文件。当您登录并浏览网站时,网站会在您的系统中存储身份验证 Cookie 。由于 cookie 存储在默认 Firefox 配置文件的本地存储中,即使在重新打开浏览器时您也会自动进行身份验证。
但是,当GeckoDriver为您的测试启动一个新的 Web 浏览会话时,每次启动Firefox时都会创建一个临时的新mozprofile,这从以下日志中可以明显看出:
mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.fDJt0BIqNu0n"
您可以在 Is it Firefox or Geckodriver 中找到详细讨论,它创建了“rust_mozprofile”目录
一旦测试执行完成并被quit()调用,临时mozprofile将在以下过程中删除:
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
因此,当您使用Selenium、GeckoDriver和Firefox打开同一页面时,无法访问存储在默认 Firefox 配置文件的本地存储中的 cookie,因此您将被重定向到登录页面。
要在本地存储中存储和使用 cookie以自动进行身份验证,您需要创建和使用自定义 Firefox 配置文件。
在这里您可以找到有关 webdriver.FirefoxProfile() 的相关讨论:是否可以使用配置文件而不复制它?