diff --git a/devtools/browser.py b/devtools/browser.py index 63721fb8..45a38672 100644 --- a/devtools/browser.py +++ b/devtools/browser.py @@ -7,9 +7,12 @@ import signal import tempfile +from .system import which_browser + +default_path=which_browser() class Browser: - def __init__(self, debug=None, path=None, headless=True): + def __init__(self, debug=None, path=default_path, headless=True): self.pipe = Pipe() self.temp_dir = tempfile.TemporaryDirectory() @@ -20,14 +23,6 @@ def __init__(self, debug=None, path=None, headless=True): else: stderr = debug - if not path: - if platform.system() == "Windows": - path = r"C:\Program Files\Google\Chrome\Application\chrome.exe" - elif platform.system() == "Linux": - path = "/usr/bin/google-chrome-stable" - else: - raise ValueError("You must set path to a chrome-like browser") - new_env = os.environ.copy() new_env["CHROMIUM_PATH"] = path new_env["USER_DATA_DIR"] = self.temp_dir.name diff --git a/devtools/system.py b/devtools/system.py new file mode 100644 index 00000000..8c621cff --- /dev/null +++ b/devtools/system.py @@ -0,0 +1,21 @@ +import shutil +import platform +import os + +chrome = ["chrome", "Chrome", "google-chrome", "google-chrome-stable", "chromium"] +chromium = ["chromium"] +# firefox = // this needs to be tested +# brave = // this needs to be tested +# edge = // this needs to be tested + +def which_browser(executable_name = chrome): + path = None + if isinstance(executable_name, str): + executable_name = [executable_name] + for exe in executable_name: + if platform.system() == "Windows": + os.environ['NoDefaultCurrentDirectoryInExePath']=0 + path = shutil.which(exe) + if path: break + # if not path, error + return path