如何解决如何使用 Selenium 打开 Edge 浏览器以保持登录状态
我现在使用 Selenium 打开 Edge 浏览器。它运行良好,但我发现我无法保持登录状态。我该如何解决这个问题?
解决方法
您无法使用 Selenium 保持登录状态,因为它会在您每次启动浏览器时创建一个新配置文件。您可以每次使用相同的配置文件启动 Edge 以保持登录状态。
示例代码:
from msedge.selenium_tools import Edge,EdgeOptions
edge_options = EdgeOptions()
edge_options.use_chromium = True
#Here you set the path of the profile ending with User Data not the profile folder
edge_options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Microsoft\\Edge\\User Data");
#Here you specify the actual profile folder
edge_options.add_argument("profile-directory=Profile 1");
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = Edge(options = edge_options,executable_path = "your\\path\\to\\edge\\webdriver\\msedgedriver.exe")
driver.get('https://google.com')
driver.quit()
以上代码使用 MS Edge Selenium 工具。您需要运行以下命令进行安装:
pip install msedge-selenium-tools selenium==3.141
请注意将代码中的所有路径更改为您自己的路径。如果您不知道特定配置文件的路径,您可以检查 edge://version/,如下所示:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。