首页 > 解决方案 > 使用 Selenium 实现边缘自动化 - 不断需要凭据

问题描述

我想在公司网络内使用 Selenium 和 Python 打开例如带有 Edge 的 Google 页面。

from selenium import webdriver
browser = webdriver.Edge(r"C:/_path_to_driver_/msedgedriver.exe")
browser.get("https://www.google.com")

Edge 打开此站点并要求我插入我的邮件。 在此处输入图像描述

输入邮件后,它会将我重定向到此页面: 在此处输入图像描述

我必须始终手动单击确定。Selenium 无法单击该确定。知道如何执行点击吗?或者有办法如何存储我的邮件地址或证书,而不是总是要求它?此问题仅发生在由 selenium 控制的 Edge 上。默认 Edge 会记住所有设置。

标签: pythonwindowsseleniummicrosoft-edgecredentials

解决方案


如果我们在没有任何参数的情况下使用 selenium webdriver 来自动化 Edge,它每次都会创建一个新的配置文件,而不是使用现有的用户数据配置文件。这就是为什么它每次都要求提供凭据。

作为一种解决方法,您可以使用user-data-dirprofile-directory使用特定配置文件来使用 Selenium WebDriver 启动 Edge。您可以使用存储凭据的配置文件,以便在您自动化 Edge 时不会要求提供凭据。我认为在您的情况下,它是默认的 Edge 配置文件。

您需要使用命令安装 MS Edge Selenium 工具pip install msedge-selenium-tools selenium==3.141,然后参考以下示例代码:

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 2");

driver = Edge(options = edge_options, executable_path = r"C:\_path_to_driver_\msedgedriver.exe")
driver.get('https://www.google.com')

注意:将代码中的路径更改为您自己的。

如果您不知道特定配置文件的路径,您可以检查edge://version/如下:

在此处输入图像描述


推荐阅读