Selenium 驱动程序挂在操作系统警报上

Jes*_*ers 5 python selenium python-3.x selenium-chromedriver selenium-webdriver

我在 Python (3.11) 中使用 Selenium 和 Firefox (107) 驱动程序。

通过驱动程序,我导航到一个页面,经过几次操作后,该页面触发操作系统警报(提示我启动程序)。当此警报弹出时,驱动程序会挂起,只有手动关闭后,我的脚本才会继续运行。

我已经尝试过driver.quit(),以及使用

os.system("taskkill /F /pid " + str(process.ProcessId))
Run Code Online (Sandbox Code Playgroud)

与驱动程序的PID,没有运气。

我已经设法防止弹出窗口弹出

options.set_preference("security.external_protocol_requires_permission", False)
Run Code Online (Sandbox Code Playgroud)

但代码仍然以相同的方式挂在弹出窗口弹出的位置。

我不在乎程序是否启动,我只需要我的代码在这个关键点不需要人工干预。

这是我目前拥有的一个最小示例:

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()
Run Code Online (Sandbox Code Playgroud)

Guy*_*Guy 3

您可以尝试一些首选项

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)
Run Code Online (Sandbox Code Playgroud)