Sunday 27 November 2022

Selenium driver hanging on OS alert

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


from Selenium driver hanging on OS alert

No comments:

Post a Comment