首页 > 解决方案 > 使用 selenium python 在 Edge 上的用户配置文件

问题描述

每次我使用 selenium 打开边缘时,它都会打开一个新的配置文件。如何设置我自己的个人资料?

标签: pythonseleniummicrosoft-edge

解决方案


您可以使用以下代码打开具有特定配置文件的 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/如下所示:

在此处输入图像描述


推荐阅读