简体   繁体   中英

How do I run a test to check if Google Chrome is running (python)

I've been working on a script to automate some things in chrome, and i've gotten stuck on the simplest task. How do I run a test to check if Chrome is currently running on my pc? I was previously using pywinautos find_windows, but that only checks the tab name.

something like:

def chrome_running():
        if 'google-chrome' running:
            return True
        else:
            return False

I'm willing to use any modules. Thanks!

Try psutil module as following:-

import psutil

def if_process_is_running_by_exename(exename='chrome.exe'):
    for proc in psutil.process_iter(['pid', 'name']):
        # This will check if there exists any process running with executable name
        if proc.info['name'] == exename:
            return True
    return False

This is checked on Window machine. You can play around with the executable name to make it work on other OSs also. Reference:- https://pypi.org/project/psutil/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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