Skip to content

Commit

Permalink
Add function which() to find variations of chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
ayjayt committed Aug 20, 2024
1 parent 63e38c4 commit ad00073
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
13 changes: 4 additions & 9 deletions devtools/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions devtools/system.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ad00073

Please sign in to comment.