popen.kill 没有关闭浏览器 window

popen.kill not closing browser window

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

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

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