如何解决Selenium + Python:Microsoft Edge Alert - 没有这样的警报
在 Selenium 上运行进程时,我收到一个弹出窗口(似乎是 Edge 浏览器的本机)。尝试以下操作时:browser.switch_to.alert.accept()
,
我收到错误selenium.common.exceptions.NoAlertPresentException: Message: no such alert
我试过等一段时间让弹出窗口出现,并尝试使用 Edge 选项无济于事。
我必须的另一个想法是模拟按下左箭头键,然后在弹出窗口变为活动状态时输入,但是我需要在 selenium 上选择一个特定元素并且没有找到弹出窗口。
from msedge.selenium_tools import Edge,EdgeOptions
options = EdgeOptions()
options.add_experimental_option("prefs",{
"profile.default_content_setting_values.notifications": 1
})
browser = Edge(options=options)
也许这是一种不同类型的警报?
解决方法
我现在明白为什么你最初在这个问题上悬赏 50 分了,因为这是一个通过 Selenium 解决的非常复杂的问题。
已解决 -- 02/05/2021
这应该不难解决!!我对多个论坛上关于这个问题的所有帖子感到非常惊讶。解决这个问题需要很多路径和错误。
第 1 步:
打开 Microsoft Edge 并导航到调用对话框的站点,如下所示。
第 2 步:
选中该框 - 允许在关联的应用程序中打开此类链接。单击“打开”。关闭关联的应用并关闭 Microsoft Edge。
第 3 步:
重新打开 Microsoft Edge 并导航到 edge://settings/content/applicationLinks
现在允许应用程序(例如缩放)自动打开。
第 4 步:
关闭 Microsoft Edge 并使用以下代码启动 Selenium:
from msedge.selenium_tools import Edge
from msedge.selenium_tools import EdgeOptions
edge_driver = '/usr/local/bin/msedgedriver'
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_argument("user-data-dir=/Users/user_name/Library/Application Support/Microsoft Edge/User Data")
edge_options.add_argument("profile-directory=Profile 1")
edge_options.add_experimental_option("useAutomationExtension",False)
edge_options.add_experimental_option("excludeSwitches",['enable-automation'])
# I needed this to work on macOS
edge_capabilities = edge_options.capabilities
edge_capabilities["platform"] = "ANY"
edge_capabilities.update(edge_options.to_capabilities())
driver = Edge(executable_path=edge_driver,options=edge_options)
driver.get("https://zoom.us/j/000-000-000")
缩放应用程序将在没有对话框的情况下自动打开。
似乎 msedge 或 selenium 每次启动时都会创建一个新的配置文件,这种情况仍然发生,但现在配置文件 1 的参数正在被继承。
更新 -- 02/04/2021
根据大量研究,似乎这个对话框不能在 Selenium 控制 Google Chrome 时被处理>、Microsoft Edge 或 Mozilla Firefox。
可以使用多种方法在 Selenium 之外抑制对话框,包括运行多个版本 Windows 的 Microsoft 平台上的注册表设置、运行 macOS 的 Apple 平台上的属性列表 (PLIST) 甚至浏览器级别切换。
但到目前为止,这些方法都不适用于硒
此对话框是在操作系统 (OS) 级别的浏览器(和 Selenium)之外生成的。使用 Selenium 时,似乎必须在操作系统级别处理此对话框。
这些模块可能有助于在操作系统级别处理此对话框。
- win32gui - 微软
- win32com - 微软
- appscript - 苹果
我在 Github 上的 SeleniumHQ/selenium 下发现了一个与您类似的问题。 问题 AddUserProfilePreference: Protocol_handler 已关闭且从未解决。
原始帖子 -- 2021 年 1 月 31 日
我已经厌倦了绕过对话框:
driver.switch_to.alert.accept()
但是这个调用会抛出这个错误:
selenium.common.exceptions.NoAlertPresentException: Message: no such alert (Session info: MicrosoftEdge=88.0.705.56)
我尝试使用 EdgeOptions() 单独和成组传递这些选项:
edge_options.add_argument("--disable-popup-blocking")
edge_options.add_argument('--disable-default-apps')
edge_options.add_argument('--allow-silent-push')
edge_options.add_argument('--disable-notifications')
edge_options.add_argument('--suppress-message-center-popups')
edge_options.add_argument('--inprivate')
但对话框仍然出现。
我也试过这个:
from msedge.selenium_tools import Edge
from msedge.selenium_tools import EdgeOptions
edge_driver = '/usr/local/bin/msedgedriver'
edge_options = EdgeOptions()
edge_options.use_chromium = True
prefs = {'profile.default_content_setting_values.notifications': 2}
edge_options.add_experimental_option('prefs',prefs)
edge_capabilities = edge_options.capabilities
# I needed this to work on macOS
edge_capabilities["platform"] = "ANY"
edge_capabilities.update(edge_options.to_capabilities())
driver = Edge(executable_path=edge_driver,capabilities=edge_capabilities)
driver.get("https://zoom.us/j/000-000-000")
但对话框再次弹出。
我还尝试编辑默认的 Microsoft Edge 配置文件。在此编辑期间,我允许我的测试站点 (zoom.us) 执行大多数操作。
from msedge.selenium_tools import Edge
from msedge.selenium_tools import EdgeOptions
edge_driver = '/usr/local/bin/msedgedriver'
edge_options = EdgeOptions()
edge_options.use_chromium = True
edge_options.add_argument("user-data-dir=/Users/user_name/Library/Application Support/Microsoft Edge/User Data")
edge_capabilities = edge_options.capabilities
# I needed this to work on macOS
edge_capabilities["platform"] = "ANY"
edge_capabilities.update(edge_options.to_capabilities())
driver = Edge(executable_path=edge_driver,capabilities=edge_capabilities)
driver.get("https://zoom.us/j/000-000-000")
但对话框仍然弹出,但缩放应用程序已下载,这在以前没有发生过。
我还使用 edge_options.add_argument('switch_name') 测试了许多这样的 switches,但和以前一样,没有任何效果。
所以就像我之前说的,这个对话框不是一个普通的警报,它可能需要在 Selenium 之外的代码或设置。我正在尝试确定需要采取哪些额外措施。
在我看来,您似乎需要找到一个 python 模块,甚至是一个可以确定 PID 或该弹出窗口的焦点的操作系统应用程序。一旦您确定通过操作系统您应该可以关闭对话框。
我无法测试这个 Microsoft Windows 示例,它可能会停止每个站点的对话。
在此 Microsoft Forum discussion 中,用户正在讨论如何阻止这些警报在 Microsoft Edge 浏览器中发生。用户建议为 MacOS 启用 PLIST 或为 Microsoft OS 启用注册表项。
我测试了 PLIST,它可以在 Microsoft Edge 浏览器中运行,但前提是它不受 selenium 控制。
更新 -- 02-04-2021
我也测试了这些开关,但没有解决对话框问题。
# references
#
# 1. https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc
#
# 2. https://stackoverflow.com/questions/62926156/chrome-84-a-website-wants-to-open-this-application-handlers
#
# 3. https://support.google.com/chrome/a/thread/62872506?hl=en
#
# 4. https://bugs.chromium.org/p/chromium/issues/detail?id=982341
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities().CHROME
prefs = {"protocol_handler": {"allowed_origin_protocol_pairs": {"https://zoom.us/*": {"zoom.us": True}}}}
# prefs = {"protocol_handler": {"excluded_schemes": {"zoom": False}}}
# prefs = {"protocol_handler": {"policy.auto_launch_protocols_from_origins": {"https://zoom.us/*": {"zoom.us": True}}}}
# prefs = {"AutoLaunchProtocolsFromOrigins": [{"allowed_origins": ["*"],"protocol":"zoom.us"}]}
chrome_options.add_experimental_option('prefs',prefs)
capabilities.update(chrome_options.to_capabilities())
旁注:
我能够使用以下开关禁用横幅“Microsoft Edge 由自动化测试软件控制”:
edge_options.add_experimental_option("useAutomationExtension",['enable-automation'])
----------------------------------------
My system information
----------------------------------------
Platform: macOS
Python: 3.8.0
msedge-selenium-tools: 3.141.2
----------------------------------------
,
options.add_experimental_option("prefs",{
"profile.default_content_setting_values.notifications": 1
})
您正在启用通知将其更改为 2 或完全删除它
警报也仅适用于通过 windows.alert() 抛出的警报
看到的警报来自 chrome 或浏览器,而不是网站本身。所以你不能使用 switch 来提醒
,不太确定,但可能浏览器不信任该程序并需要用户的许可-因此它不允许您使用该程序接受。
您对按键模拟的想法可能会奏效,请使用我很确定是默认键盘库,但可能不是,
只是模拟你的钥匙,也就是说,希望能以某种方式有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。