如何以编程方式检查Windows机器上打开的浏览器实例? [英] How do I programmatically check for open browser instances on a Windows machine?
问题描述
我写了Python中的GUI自动化脚本,打开一个网页浏览器,做了一些测试,在浏览器中,然后关闭浏览器。它这样做是在一个循环,通过数十万迭代往上走。
我碰到的问题,该程序会挂起,我会得到任何浏览器实例中打开或有时多达20个实例打开。
我想创建一个错误检查,以确保没有在同一时间只有一个浏览器实例打开(彻底杀灭浏览器,如果有很多实例中打开重启迭代和/或确保浏览器的实例是开放前通过去循环迭代)。
的PID浏览器通过循环的每一次迭代变化明显而据我所知,是没有办法来分配的PID。
我还以为是使系统调用到终端并以某种方式利用任务列表,并使用taskkill命令,但另一种解决方案仍然没有想出一个办法做我想做的。
任何建议将是极大的AP preciated!
更新:
我明白,我可以按名称搜索窗口过程,但我会如何将其写入一个布尔值前pression像
如果任务列表中包含firefox.exe
TASKKILL firefox.exe
其他
打开Firefox
第二次更新:
我有我能够在Ruby中code其中工程如下:
如果系统(任务列表|查找firefox.exe')==真
a = 1时
ELSIF系统(任务列表|查找firefox.exe')==假
a = 0时
结束
我怎么会在Python做到这一点?
答案我一直在寻找:
B =(STR(使用os.system('任务列表|查找firefox.exe')))
若B =='1':
B =假
ELIF b ==0:
B =真
印片b
我一直都在做类似的事情。
我觉得你可能要pywinauto
从pywinauto.findwindows进口find_windowsfind_windows(best_match ='YOURWINDOWNAMEHERE')
这将引发异常,如果YOURWINDOWNAMEHERE没有打开
编辑:其实对于Firefox或Chrome,它只能当YOURWINDOWNAMEHERE就是不管你是在页面比如这个页面是蟒蛇 - 如何编程......
如果它发现它,它会返回一个int,据我知道这INT应该是能够引用到窗口。
例如:
>>> find_windows(best_match =计算器)#此得到一个异常#::现在我手动打开计算器::>>> find_windows(best_match =计算器)[12345] #orwhatever进程号它会
I wrote a GUI automation script in python that opens a web browser, does some tests in the browser and then closes the browser. It does so in a loop going up through hundreds of thousands of iterations.
I've run into problems where the program would hang and I would get no browser instances open or sometimes up to 20 instances open. I would like to create an error check to make sure there is only one browser instance open at a time (kill the browser completely and restart the iteration if there are many instances open and/or make sure a browser instance is open before going through an iteration of the loop).
The PID for the browser obviously changes through every iteration of the loop and as far as I know, there is no way to assign PIDs. Another solution I thought of is making a system call to the terminal and somehow utilizing the tasklist and taskkill commands but have still not figured out a way to do what I want.
Any suggestions would be greatly appreciated!
Update:
I understand I can search windows processes by name but how would I put that into a boolean expression like
if tasklist contains firefox.exe
taskkill firefox.exe
else
open firefox
Second Update:
I have the following that I was able to code in Ruby which works:
if system('tasklist | find "firefox.exe"') == true
a = 1
elsif system('tasklist | find "firefox.exe"') == false
a = 0
end
How would I accomplish this in Python?
Answer I was looking for:
b = (str(os.system('tasklist | find "firefox.exe"')))
if b == '1':
b = 'false'
elif b == '0':
b = 'true'
print b
Ive been doing similar things
i think you may want pywinauto
from pywinauto.findwindows import find_windows
find_windows(best_match='YOURWINDOWNAMEHERE')
this will raise an exception if YOURWINDOWNAMEHERE is not open
edit: actually for firefox or chrome, it only works if "YOURWINDOWNAMEHERE" is whatever the page you are on is for example this page is "python - How do i programmatically ..."
if it finds it, it will return an int, as far as i know this int is supposed to be able to reference to the window.
example:.
>>>find_windows(best_match="calculator") #this gets an exception
# ::I manually open Calculator now ::
>>>find_windows(best_match="calculator")
[12345] #orwhatever process number it gets
这篇关于如何以编程方式检查Windows机器上打开的浏览器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!