Selenium + Python:Microsoft Edge 警报 - 没有此类警报

lik*_*ble 6 python selenium alert popup

在 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

也许这是一种不同类型的警报?

Lif*_*lex 3

我现在明白为什么您最初在这个问题上提供 50 分的赏金了,因为这是一个通过 Selenium 解决的非常复杂的问题。

已解决 -- 2021 年 2 月 5 日

这本来不应该这么难解决!我对多个论坛上关于这个问题的所有帖子感到非常惊讶。解决这个问题需要很多尝试和错误。

步骤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")
Run Code Online (Sandbox Code Playgroud)

缩放应用程序将自动打开,无需对话框。

似乎 msedge 或 selenium 每次启动时都会创建一个新的配置文件,这种情况仍然会发生,但现在配置文件 1 中的参数正在被继承。

更新 -- 02/04/2021

根据大量研究,当 Selenium 控制Google ChromeMicrosoft EdgeMozilla Firefox时,似乎无法处理此对话框。

可以使用多种方法在 Selenium 之外抑制该对话框,包括运行多个 Windows 版本的 Microsoft 平台上的注册表设置、运行 macOS 的 Apple 平台上的属性列表 (PLIST),甚至浏览器级别开关。

但到目前为止,这些方法都不适用于 Selenium

该对话框是在操作系统 (OS) 级别的浏览器(和 Selenium)外部生成的。使用 Selenium 时,您似乎必须在操作系统级别处理此对话框。

这些模块可能有助于在操作系统级别处理此对话框。

  1. win32gui-微软
  2. win32com-微软
  3. appscript - 苹果

我在Github上的SeleniumHQ/selenium下发现了与您类似的问题。问题AddUserProfilePreference: Protocol_handler已关闭且从未解决。

原帖 -- 01/31/2021

我厌倦了绕过对话框:

driver.switch_to.alert.accept()
Run Code Online (Sandbox Code Playgroud)

但是这个调用会抛出这个错误:

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')
Run Code Online (Sandbox Code Playgroud)

但对话框仍然出现。

我也尝试过这个:

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")
Run Code Online (Sandbox Code Playgroud)

但再次弹出对话框。

我还尝试编辑默认的 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")
Run Code Online (Sandbox Code Playgroud)

但对话框仍然弹出,但缩放应用程序已下载,这是以前没有发生过的情况。

我还使用edge_options.add_argument('switch_name')测试了数十个这样的开关,但就像以前一样没有任何效果。

正如我之前所说,这个对话框不是一个普通的警报,它可能需要 Selenium 之外的代码或设置。我正在尝试确定需要采取哪些额外措施。

对我来说,你似乎需要找到一个 python 模块甚至一个操作系统应用程序来确定 PID 或该弹出窗口的焦点。一旦您可以通过操作系统确定,您应该能够关闭该对话框。

我无法测试这个Microsoft Windows示例,该示例应该会停止每个站点的对话。

在此Microsoft 论坛讨论中,用户正在讨论如何阻止 Microsoft Edge 浏览器中发生这些警报。用户建议启用适用于 MacOS 的 PLIST 或适用于 Microsoft 操作系统的注册表项。

我测试了 PLIST,它可以在 Microsoft Edge 浏览器中运行,但仅当它不受selenium控制时才有效。


更新 -- 02-04-2021

我也测试了这些开关,但没有解决对话框问题。

# references
# 
# 1. https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc
#
# 2. /sf/ask/4404830951/
#
# 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())

Run Code Online (Sandbox Code Playgroud)

边注:

我可以使用以下开关禁用“Microsoft Edge 正在由自动化测试软件控制”的横幅:

edge_options.add_experimental_option("useAutomationExtension", False)
edge_options.add_experimental_option("excludeSwitches", ['enable-automation'])
Run Code Online (Sandbox Code Playgroud)
----------------------------------------
My system information
----------------------------------------
Platform:               macOS
Python:                 3.8.0
msedge-selenium-tools:  3.141.2
----------------------------------------
Run Code Online (Sandbox Code Playgroud)