Selenium driver hanging on OS alert
Asked Answered
D

3

5

I'm using Selenium in Python (3.11) with a Firefox (107) driver.

With the driver I navigate to a page which, after several actions, triggers an OS alert (prompting me to launch a program). When this alert pops up, the driver hangs, and only once it is closed manually does my script continue to run.

I have tried driver.quit(), as well as using

os.system("taskkill /F /pid " + str(process.ProcessId))

with the driver's PID, with no luck.

I have managed to prevent the pop-up from popping up with

options.set_preference("security.external_protocol_requires_permission", False)

but the code still hangs the same way at the point where the popup would have popped up.

I don't care whether the program launches or not, I just need my code to not require human intervention at this key point.

here is a minimal example of what I currently have:

from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.firefox.options import Options
from seleniumwire import webdriver

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options.set_preference("security.external_protocol_requires_permission", False)
driver = webdriver.Firefox(options=options)

# Go to the page
driver.get(url)

user_field = driver.find_element("id", "UserName")
user_field.send_keys(username)
pass_field = driver.find_element("id", "Password")
pass_field.send_keys(password)
pass_field.send_keys(Keys.ENTER)

#this is the point where the pop up appears

reqs = driver.requests

print("Success!")
driver.quit()
Dramaturge answered 24/11, 2022 at 16:9 Comment(4)
For me, there is a halt, but the code continues to execute normally after a few seconds. Is it this delay which you're trying to remove? Or does your program halt indefinitely?Unforgettable
@Unforgettable Mine halts indefinitely. I even left it over my lunch break just to be sure.Dramaturge
What is the dialog (pop-up) that shows after sending your credentials? Why don't you close the dialog with code?Pyroxenite
The dialogue is almost identical to a screenshot posted in the comments. I don't know how to close it with code, that's the essence of my questionDramaturge
E
3

There are some prefs you can try

profile = webdriver.FirefoxProfile()
profile.set_preference('dom.push.enabled', False)

# or

profile = webdriver.FirefoxProfile()
profile.set_preference('dom.webnotifications.enabled', False)
profile.set_preference('dom.webnotifications.serviceworker.enabled', False)
Eades answered 28/11, 2022 at 13:46 Comment(0)
A
3

Have you tried setting this preference to prevent the particular popup:

profile.set_preference('browser.helperApps.neverAsk.openFile', 'typeOfFile')  
# e.g. profile.set_preference('browser.helperApps.neverAsk.openFile', 'application/xml,application/octet-stream')

Or have you tried just dismissing the popup:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

....
pass_field.send_keys(Keys.ENTER)

#this is the point where the pop up appears
WebDriverWait(driver, 5).until(EC.alert_is_present).dismiss()
reqs = driver.requests
...
Aristaeus answered 28/11, 2022 at 17:56 Comment(3)
I have not tried either of those, but they lookpromising!Dramaturge
Let me know how it goes. If not maybe you can provide me with more code so that I can re-create the issue.Aristaeus
Selenium doesn't recognize that kind of alerts, the WebDriverWait won't work here.Eades
R
1

check this checkbox manually then open the app for every app associated to the links you use, then it will work normally.

enter image description here

Radiate answered 27/11, 2022 at 15:57 Comment(4)
HI, I tried this, and either Firefox does not truly remember or it doesn't work.Dramaturge
To get that to work, you might need to use a profile in Firefox. Selenium is starting with a fresh profile per default. Here I found an answer to load the default profile: https://mcmap.net/q/66885/-how-can-i-set-a-default-profile-for-the-firefox-driver-in-selenium-webdriver-3Creamery
@Creamery how would I extract/export the Firefox profile that contains this checkbox change?Dramaturge
Here you'll find the profiles and which file contains your change: support.mozilla.org/en-US/kb/….Creamery

© 2022 - 2024 — McMap. All rights reserved.