Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

Selenium + Python: Microsoft Edge Alert - no such alert

When running a process on Selenium, I get a pop-up (which appears to be native to the Edge browser). When trying the following:
browser.switch_to.alert.accept(),
I get the error
selenium.common.exceptions.NoAlertPresentException: Message: no such alert

I've tried waiting some time for the pop up to appear, as well as messing around with the Edge options to no avail.

Another idea I have to is simulate pressing the left arrow key then enter as the pop-up becomes active, however I need to select a specific element on selenium and the pop-up is not found.

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)

enter image description here

Perhaps this is a different type of alert?

question from:https://stackoverflow.com/questions/65846833/selenium-python-microsoft-edge-alert-no-such-alert

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can now see why you initially offered a 50 point bounty on this question, because it is a very complex problem to solve via Selenium.

SOLVED -- 02/05/2021

This should have not been this difficult to solve!! I'm totally amazed at all of the posts across multiple forums on this issue. Solving this took lot of trails and errors.

Step 1:

Open Microsoft Edge and navigate to a site that invokes the dialog as shown below.

enter image description here

Step 2:

Check the box - Allows allow to open links of this type in the associated app. Click Open. Close the associated app and shutdown Microsoft Edge.

Step 3:

reopen Microsoft Edge and navigate to edge://settings/content/applicationLinks

The application(e.g., zoom) is now allowed to automatically open.

enter image description here

Step 4:

Close Microsoft Edge and launch Selenium with the following code:

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

The zoom application will automatically open without the dialog.

It seems that either msedge or selenium is creating a new profile each time it was launched, which still happens, but now parameters from Profile 1 are being inherited.

UPDATE -- 02/04/2021

Based on lots of research it seems that this dialog CANNOT be handled when Selenium is controlling either Google Chrome, Microsoft Edge or Mozilla Firefox.

The dialog can be suppressed outside of Selenium using a variety of methods, including registry settings on Microsoft platforms running multiple versions of Windows, Property list(PLIST) on Apple platforms running macOS and even browser level switches.

BUT SO FAR NONE OF THESE METHODS HAVE WORK WITH SELENIUM

This dialog is being generated outside the browser (and Selenium) at the Operating System (OS) level. It seem that you have to deal with this dialog at the OS level when using Selenium.

These modules might be useful in dealing with this dialog at the OS level.

  1. win32gui - Microsoft
  2. win32com - Microsoft
  3. appscript - Apple

I found an issue similar to yours on Github under SeleniumHQ/selenium. The issue AddUserProfilePreference: Protocol_handler was closed and never resolved.

ORIGINAL POST -- 01/31/2021

I have tired to bypass the dialog with:

driver.switch_to.alert.accept()

But this call throws this error:

selenium.common.exceptions.NoAlertPresentException: Message: no such alert (Session info: MicrosoftEdge=88.0.705.56)

I have tried to pass these options individually and in groups with 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')

But the dialog still shows up.

I have also tried this:

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

But yet again the dialog pops-up.

I have also tried to edit the default Microsoft Edge profile. During this edit I allowed my test site (zoom.us) to do most things.

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

But the dialog still popped up, but the zoom application downloaded, which didn't happen before.

I have also tested dozens of these switches using edge_options.add_argument('switch_name'), but like before nothing worked.

So like I stated before this dialog isn't a normal Alert and it will likely require code or setting outside of Selenium. I'm trying to determine what additional measures are needed.

To me it seems that you need to find a python module or even an OS application that can determine either the PID or the focus on that popup. Once you can determine that via the OS you should be able to close the dialog.

I cannot test this Microsoft Windows example, which supposedly stops the dialog per site.

In this Microsoft Forum discussion users are discussing how to stop these alerts from happening in a Microsoft Edge browser. The users are suggesting enabling a PLIST for MacOS or a registry key for Microsoft OS.

I tested the PLIST, which worked in the Microsoft Edge browser, but only when it wasn't being controlled by selenium.


UPDATE -- 02-04-2021

I also tested these switches, which did not solve the dialog issue.

# 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())

SIDE NOTE:

I was able to disable the banner "Microsoft Edge is being controlled by automated test software" using the switches below:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...