5

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?

likethevegetable
  • 264
  • 1
  • 4
  • 17
  • 2
    Check https://stackoverflow.com/questions/29554564/bypass-external-protocol-request-popup-during-selenium-automation – Rahul L Jan 25 '21 at 05:17
  • I can now see why you offered a ***50 point bounty*** on this question. – Life is complex Feb 03 '21 at 19:44
  • I did some more research into this issue with other web browsers that were being controlled by selenium. This is a consistent "feature" when using selenium – Life is complex Feb 03 '21 at 19:45
  • After looking at your problem from all sides I would recommend opening an issue on GitHub with the owner of selenium. I attempted to add a PLIST for msedge and selenium like I did for Edge in my answer, but that PLIST didn't work. I would link your GitHub issue to this question. – Life is complex Feb 03 '21 at 19:45
  • @Lifeiscomplex, thanks for the suggestion. I'll likely do so. My ultimate solution was to run a Shell script to switch windows to Egde (not ideal), and then perform arrow key presses. A bit hokey and sometimes fails, but mostly works – likethevegetable Feb 04 '21 at 14:02
  • @likethevegetable I'm not sure if this issue is solvable using selenium. I have looked at a lot of posts concerning this issue in newer versions of browsers, including Google Chrome, Microsoft Edge and Mozilla Firefox. This topic is even debated in chromium issue tracking. Most people struggle to bypass this dialog and it seems to be linked to some "security feature", which supposedly used to mitigate hijacking. – Life is complex Feb 04 '21 at 20:27
  • I found an issue similar to yours on *Github* under *SeleniumHQ/selenium*. The issue [AddUserProfilePreference: Protocol_handler](https://github.com/SeleniumHQ/selenium/issues/8618) was closed and never resolved. – Life is complex Feb 05 '21 at 03:51
  • I was finally able to solved this issue without using any additional Python modules. Please let me know when you try my solution that worked using Microsoft Edge being operated by selenium. – Life is complex Feb 06 '21 at 16:31

5 Answers5

4

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
----------------------------------------
Life is complex
  • 15,374
  • 5
  • 29
  • 58
1
options.add_experimental_option("prefs", {
    "profile.default_content_setting_values.notifications": 1
})

you are enabling notifications change it to 2 or completely remove that

Also alert works only for alert thrown through windows.alert()

The alert seeing is from chrome or browser not the website itself. So you cannot use switch to alert

PDHide
  • 18,113
  • 2
  • 31
  • 46
1

Not too sure about it but perhaps the browser isn't trusting the program and wants the user's permission- so it doesn't let you use the program to accept.
Your idea with the key simulation might work, use the keyboard library which i'm pretty sure is a default one, it might not be, and just simulate your keys and that is, hope that helps somehow.

0

If anyone experiencing the same protocol issue with msteams protocol. i found a workaround for this by fetching a suppressed URI to the meeting which doesn't show the prompt to open msteams desktop client, so no xdg-open prompt. Python solution:

# Open the original meeting url, which shows the xdg-open prompt.
            driver.get(url)
            time.sleep(2)
            # Suppressed uri doesn't has window prompt to open msteams app,
            # which breaks webdriver.
# The suppressed uri (no prompt) lies inside iframe
            meeting_uri = driver.find_element_by_id("teamsLauncher").get_property("baseURI")
# open the uri in a new browser tab
            driver.execute_script("window.open();")
# Switch to the new tab
            new_tab = driver.window_handles[-1]
            driver.switch_to.window(new_tab)
            driver.get(meeting_uri)
dbecks7
  • 1
  • 1
0

I was facing same issue, but it worked with below fix. You need to Change #EDIT ME place holders in the pseudo code #2

And thanks to @Life is complex


1.First you need to install "msedge-selenium-tools"

pip install msedge-selenium-tools

2.Generate driver object with EdgeOptions()

from msedge.selenium_tools import Edge
from msedge.selenium_tools import EdgeOptions

# EDIT ME : CONFIGURE YOUR msedgedriver.exe location, download from : [https://msedgewebdriverstorage.z22.web.core.windows.net/?prefix=113.0.1774.57/][1]
edge_driver = r'C:\\Users\<<LOCAL_FOLDER_PATH>>\\msedgedriver'

edge_options = EdgeOptions()
edge_options.use_chromium = True

# EDIT ME : Change USER_NAME, or point it to Edge User Data folder ( This is to re-use existing saved options )
edge_options.add_argument("user-data-dir=C:\\Users\\<<USER_NAME>>\\AppData\\Local\\Microsoft\\Edge\\User Data")
edge_options.add_argument("profile-directory=Default")
edge_options.add_argument("--no-sandbox")
edge_options.add_argument("--disable-dev-shm-usage")

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)
its myview
  • 21
  • 4