python - 使用 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")
我必须始终手动单击确定。Selenium 无法单击该确定。知道如何执行点击吗?或者有办法如何存储我的邮件地址或证书,而不是总是要求它?此问题仅发生在由 selenium 控制的 Edge 上。默认 Edge 会记住所有设置。
解决方案
如果我们在没有任何参数的情况下使用 selenium webdriver 来自动化 Edge,它每次都会创建一个新的配置文件,而不是使用现有的用户数据配置文件。这就是为什么它每次都要求提供凭据。
作为一种解决方法,您可以使用user-data-dir
和profile-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/
如下:
推荐阅读
- java - 有没有办法将不同的值从表格输入到 Cucumber 背景?使背景动态?
- c# - 在应用服务编辑器中部署网站期间发生错误
- python - 在 Python 中使用 Pandas 重塑数据框
- node.js - 在 Docker 中运行 node.js 脚本
- eclipse - BIRT 报表设计 - 为辅助数据集设置参数
- c - 使用 fork 时 while(wait(NULL)){} 和 while(wait(NULL) > 0){} 有什么区别
- javascript - 如果我输入今天的日期,它会在日期前一天显示我
- python - 参数“字符串”的类型不正确(预期为 str,得到列表)
- python - QListWidget不再为空后如何启用按钮
- python - 抑制绘图子图中的y轴标签,注释未对齐