-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathchrome_driver.py
86 lines (71 loc) · 2.95 KB
/
chrome_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#
# GoogleFindMyTools - A set of tools to interact with the Google Find My API
# Copyright © 2024 Leon Böttger. All rights reserved.
#
import undetected_chromedriver as uc
import os
import shutil
import platform
def find_chrome():
"""Find Chrome executable using known paths and system commands."""
possiblePaths = [
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
r"C:\ProgramData\chocolatey\bin\chrome.exe",
r"C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe",
"/usr/bin/google-chrome",
"/usr/local/bin/google-chrome",
"/opt/google/chrome/chrome",
"/snap/bin/chromium",
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
]
# Check predefined paths
for path in possiblePaths:
if os.path.exists(path):
return path
# Use system command to find Chrome
try:
if platform.system() == "Windows":
chrome_path = shutil.which("chrome")
else:
chrome_path = shutil.which("google-chrome") or shutil.which("chromium")
if chrome_path:
return chrome_path
except Exception as e:
print(f"[ChromeDriver] Error while searching system paths: {e}")
return None
def get_options():
chrome_options = uc.ChromeOptions()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
return chrome_options
def create_driver():
"""Create a Chrome WebDriver with undetected_chromedriver."""
try:
chrome_options = get_options()
driver = uc.Chrome(options=chrome_options)
print("[ChromeDriver] Installed and browser started.")
return driver
except Exception:
print("[ChromeDriver] Default ChromeDriver creation failed. Trying alternative paths...")
chrome_path = find_chrome()
if chrome_path:
chrome_options = get_options()
chrome_options.binary_location = chrome_path
try:
driver = uc.Chrome(options=chrome_options)
print(f"[ChromeDriver] ChromeDriver started using {chrome_path}")
return driver
except Exception as e:
print(f"[ChromeDriver] ChromeDriver failed using path {chrome_path}: {e}")
else:
print("[ChromeDriver] No Chrome executable found in known paths.")
raise Exception(
"[ChromeDriver] Failed to install ChromeDriver. A current version of Chrome was not detected on your system.\n"
"If you know that Chrome is installed, update Chrome to the latest version. If the script is still not working, "
"set the path to your Chrome executable manually inside the script."
)
if __name__ == '__main__':
create_driver()