【问题标题】:Selenium driver hanging on OS alertSelenium 驱动程序挂在操作系统警报上
【发布时间】:2022-11-29 02:19:31
【问题描述】:

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

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

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

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

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

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

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

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

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

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

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

【问题讨论】:

  • 对我来说,有一个暂停,但代码在几秒钟后继续正常执行。您要消除的是这种延迟吗?还是您的程序会无限期停止?
  • @Lucan Mine 无限期停止。为了保险起见,我什至把它留在了午休时间。

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

手动选中此复选框,然后为与您使用的链接关联的每个应用程序打开应用程序,然后它将正常工作。

【讨论】:

  • 嗨,我试过了,要么 Firefox 不记得了,要么不起作用。
  • 要让它工作,您可能需要在 Firefox 中使用配置文件。 Selenium 默认从一个新的配置文件开始。在这里我找到了加载默认配置文件的答案:stackoverflow.com/a/52474787/12914172
  • @r000bin我会试试的,谢谢
  • @r000bin 我将如何提取/导出包含此复选框更改的 Firefox 配置文件?
  • 在这里您可以找到配置文件以及包含您的更改的文件:support.mozilla.org/en-US/kb/…
【解决方案2】:

您可以尝试一些偏好设置

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)

【讨论】:

    【解决方案3】:

    您是否尝试过设置此首选项以防止特定的弹出窗口:

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

    或者您是否尝试过关闭弹出窗口:

    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
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 2018-10-26
      • 1970-01-01
      相关资源
      最近更新 更多