【问题标题】:popen.kill not closing browser windowpopen.kill 不关闭浏览器窗口
【发布时间】:2021-10-03 00:53:57
【问题描述】:

过去几天我一直在尝试自动化拼合 PDF 的过程,这个功能是我发现效果最好的功能。唯一的问题是没有读取 a.kill() 命令,导致函数完成后浏览器保持打开状态。保存过程完成后如何关闭浏览器窗口?

import time
import subprocess
import keyboard

def chrome():
    test = "test"
    name = "file:///C://Users//akcgo//Documents//CARB//ARBER//PDFS//Company A.pdf"
    a = subprocess.Popen("C://Program Files (x86)//Google//Chrome//Application//chrome.exe")
    time.sleep(1)
    keyboard.write(name)
    time.sleep(1)
    keyboard.press_and_release('enter')
    time.sleep(1)
    keyboard.press_and_release(['ctrl', 'p'])
    time.sleep(1)
    keyboard.press_and_release('enter')
    time.sleep(1)
    keyboard.write(test)
    time.sleep(.5)
    keyboard.press_and_release('enter')
    time.sleep(2)
    a.kill()
    # keyboard.press_and_release(['ctrl', 'w']) doesn't work either

【问题讨论】:

    标签: python subprocess popen kill


    【解决方案1】:

    您可以使用pid = a.pid 获取子进程的PID,然后调用os.kill(pid, signal.SIGTERM)os.kill(pid, signal.SIGKILL)

    【讨论】:

    • 它在说``` PermissionError: [WinError 5] Access is denied` ``
    • 尝试以管理员权限启动 Python 脚本。
    • 谢谢!没有管理员权限有没有办法关闭它?
    • 是的,可能还有另一种解决方案:只需启动os.system("Taskkill /PID %d /F" % pid),其中变量pid 包含进程ID。
    • 收到此消息:错误:找不到进程“21436”。
    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2012-07-21
    • 2017-01-16
    • 1970-01-01
    • 2013-07-24
    • 2012-09-24
    • 2013-01-08
    相关资源
    最近更新 更多