简体   繁体   English

如何检查主流网络浏览器是否退出?

[英]How to check for exit of mainstream web browsers?

This is the situation: I would like to open a browser window from python code, as in any of the following (portability is not an issue atm): 这是这种情况:我想通过python代码打开浏览器窗口,如以下任何一种情况(可移植性不是atm的问题):

subprocess.call(['xdg-open', 'file:///someFileIGenerated.html'])
... # or
pid = subprocess.Popen(...)
... # or (from the webbrowser package)
webbrowser.open(...)

I can unfortunately not just check if the pid I created is still alive, as most browser (Firefox, Chrome) will immediately exit the creating process and spawn more (ie one per tab) as they want. 不幸的是,我不能仅仅检查我创建的pid是否仍然存在,因为大多数浏览器(Firefox,Chrome)将立即退出创建过程,并根据需要生成更多(即每个选项卡一个)。

What is the most responsible (doesn't kill other browser windows) and portable (works at least with firefox and chrome under Linux) of achieving this? 实现此目标的最负责任的方法(不会杀死其他浏览器窗口)和可移植性(至少在Linux下可与firefox和chrome一起使用)?

Edit : Can ppids be used in this context? 编辑 :在这种情况下可以使用ppid吗?

You can "ping" firefox process. 您可以“ ping” firefox过程。 There is a part of my open_url shell script below I hope you find it useful 下面是我的open_url shell脚本的一部分,希望您觉得它有用

OUTPUT=`firefox -remote "ping()" 2>&1 | grep No`
if [[ "${OUTPUT}" == "Error: No running window found" ]]; then
    # firefox is not running
else
    # firefox still running;
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM