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()
Jessica Chambers
  • 1,246
  • 5
  • 28
  • 56
  • 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? – Lucan Nov 27 '22 at 17:18
  • @Lucan Mine halts indefinitely. I even left it over my lunch break just to be sure. – Jessica Chambers Nov 28 '22 at 09:51
  • What is the dialog (pop-up) that shows after sending your credentials? Why don't you close the dialog with code? – Life is complex Nov 30 '22 at 17:07
  • 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 question – Jessica Chambers Dec 05 '22 at 10:39

3 Answers3

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)
Guy
  • 46,488
  • 10
  • 44
  • 88
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
...
mr_mooo_cow
  • 1,098
  • 1
  • 6
  • 16
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

user16930239
  • 6,319
  • 2
  • 9
  • 33
  • HI, I tried this, and either Firefox does not truly remember or it doesn't work. – Jessica Chambers Nov 27 '22 at 16:50
  • 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://stackoverflow.com/a/52474787/12914172 – r000bin Nov 27 '22 at 20:28
  • @r000bin how would I extract/export the Firefox profile that contains this checkbox change? – Jessica Chambers Nov 28 '22 at 09:59
  • 1
    Here you'll find the profiles and which file contains your change: https://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data#:~:text=Firefox%20stores%20your%20profile%20folder%20in%20this%20location,folder%20as%20follows%3A%20Click%20the%20Windows%20Start%20button. – r000bin Nov 28 '22 at 11:26