Skip to content

Commit 3085d07

Browse files
committed
next try...
1 parent 382331a commit 3085d07

3 files changed

Lines changed: 60 additions & 45 deletions

File tree

.github/workflows/ci-windows.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ jobs:
3131
run: '(Get-Item "C:\Program Files\Google\Chrome\Application\chrome.exe").VersionInfo'
3232
shell: powershell
3333

34-
- name: Add Chrome to PATH
35-
run: |
36-
$chromePath = "C:\Program Files\Google\Chrome\Application"
37-
echo "Adding $chromePath to PATH"
38-
echo "$chromePath" | Out-File -Append -Encoding utf8 $env:GITHUB_PATH
39-
shell: powershell
40-
4134
- name: Upgrade pip
4235
run: |
4336
python -m pip install --upgrade pip

html2print/html2print.py

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from selenium import webdriver
2020
from selenium.webdriver.chrome.options import Options
2121
from selenium.webdriver.chrome.service import Service
22-
from webdriver_manager.core.os_manager import ChromeType, OperationSystemManager
2322

2423
__version__ = "0.0.15"
2524

@@ -40,11 +39,7 @@
4039

4140

4241
class ChromeDriverManager:
43-
def get_chrome_driver(self, path_to_cache_dir: str):
44-
chrome_browser_info = browsers.get("chrome")
45-
chrome_version = chrome_browser_info["version"]
46-
chrome_path = chrome_browser_info["path"]
47-
42+
def get_chrome_driver(self, chrome_version: str, path_to_cache_dir: str):
4843
# If Web Driver Manager cannot detect Chrome, it returns None.
4944
if chrome_version is None:
5045
raise RuntimeError(
@@ -54,9 +49,6 @@ def get_chrome_driver(self, path_to_cache_dir: str):
5449

5550
chrome_major_version = chrome_version.split(".")[0]
5651

57-
print( # noqa: T201
58-
f"html2print: Installed Chrome path: {chrome_path}"
59-
)
6052
print( # noqa: T201
6153
f"html2print: Installed Chrome version: {chrome_version}"
6254
)
@@ -110,13 +102,24 @@ def get_chrome_driver(self, path_to_cache_dir: str):
110102

111103
@staticmethod
112104
def _download_chromedriver(
113-
chrome_major_version,
105+
chrome_major_version: str,
114106
os_type: str,
115-
path_to_driver_cache_dir,
116-
path_to_cached_chrome_driver,
117-
):
107+
path_to_driver_cache_dir: str,
108+
path_to_cached_chrome_driver: str,
109+
) -> str:
118110
url = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
119-
response = ChromeDriverManager.send_http_get_request(url).json()
111+
response = ChromeDriverManager.send_http_get_request(url)
112+
if response is None:
113+
raise RuntimeError(
114+
"Could not download known-good-versions-with-downloads.json"
115+
)
116+
117+
response = response.json()
118+
if response is None:
119+
raise RuntimeError(
120+
"Could not parse known-good-versions-with-downloads.json"
121+
)
122+
assert isinstance(response, dict)
120123

121124
matching_versions = [
122125
item
@@ -191,7 +194,7 @@ def send_http_get_request(url, params=None, **kwargs) -> Response:
191194
) from last_error
192195

193196
@staticmethod
194-
def get_chrome_version():
197+
def get_browser_info() -> browsers.Browser:
195198
# Special case: GitHub Actions macOS CI machines have both
196199
# Google Chrome for Testing and normal Google Chrome installed, and
197200
# sometimes their versions are of different major version families.
@@ -225,17 +228,26 @@ def get_chrome_version():
225228
f"html2print: Google Chrome for Testing Version: {chrome_version}"
226229
)
227230

228-
return chrome_version
231+
return browsers.Browser(
232+
browser_type="chrome-for-testing",
233+
path=chrome_path,
234+
version=chrome_version,
235+
display_name="Google Chrome for Testing",
236+
)
229237
except FileNotFoundError:
230238
print("html2print: Chrome for Testing not available.") # noqa: T201
231239
except Exception as e:
232240
print( # noqa: T201
233241
f"html2print: Error getting Google Chrome for Testing version: {e}"
234242
)
243+
else:
244+
chrome_browser_info = (
245+
browsers.get("chrome-for-testing")
246+
or browsers.get("chrome")
247+
or browsers.get("chromium")
248+
)
235249

236-
os_manager = OperationSystemManager(os_type=None)
237-
version = os_manager.get_browser_version_from_os(ChromeType.GOOGLE)
238-
return version
250+
return chrome_browser_info
239251

240252

241253
def get_inches_from_millimeters(mm: float) -> float:
@@ -314,33 +326,36 @@ class Done(Exception):
314326

315327

316328
def create_webdriver(
329+
browser_info: browsers.Browser,
317330
chromedriver: Optional[str],
318331
path_to_cache_dir: str,
319332
page_load_timeout: int,
320333
debug: bool = False,
321334
) -> webdriver.Chrome:
322335
print("html2print: creating ChromeDriver service.", flush=True) # noqa: T201
336+
323337
if chromedriver is None:
324-
path_to_chrome = ChromeDriverManager().get_chrome_driver(
325-
path_to_cache_dir
338+
path_to_chrome_driver = ChromeDriverManager().get_chrome_driver(
339+
chrome_version=browser_info["version"],
340+
path_to_cache_dir=path_to_cache_dir,
326341
)
327342
else:
328-
path_to_chrome = chromedriver
329-
print(f"html2print: ChromeDriver available at path: {path_to_chrome}") # noqa: T201
343+
path_to_chrome_driver = chromedriver
344+
print( # noqa: T201
345+
f"html2print: ChromeDriver available at path: {path_to_chrome_driver}"
346+
)
330347

331348
if debug:
332349
service = Service(
333-
path_to_chrome, log_output=PATH_TO_CHROME_DRIVER_DEBUG_LOG
350+
path_to_chrome_driver, log_output=PATH_TO_CHROME_DRIVER_DEBUG_LOG
334351
)
335352
else:
336-
service = Service(path_to_chrome)
337-
338-
path_to_chrome_browser = ""
339-
path_to_chrome_browser = browsers.get("chrome")["path"]
340-
print(f"html2print: Chrome available at path: {path_to_chrome_browser}") # noqa: T201
353+
service = Service(path_to_chrome_driver)
341354

342355
webdriver_options = Options()
343-
webdriver_options.binary_location = path_to_chrome_browser
356+
# Workaround for Windows: chrome is not typically found in the PATH, so need to supply the exact binary location manually
357+
if platform.system() == "Windows":
358+
webdriver_options.binary_location = browser_info["path"]
344359
webdriver_options.add_argument("start-maximized")
345360
webdriver_options.add_argument("disable-infobars")
346361
# Doesn't seem to be needed.
@@ -457,24 +472,31 @@ def main():
457472

458473
args = parser.parse_args()
459474

475+
# Look for Chrome on the System...
476+
browser_info = ChromeDriverManager.get_browser_info()
477+
460478
path_to_cache_dir: str
461479
if args.command == "get_driver":
462480
path_to_cache_dir = (
463481
args.cache_dir if args.cache_dir is not None else DEFAULT_CACHE_DIR
464482
)
465483

484+
path_to_chrome_browser = browser_info["path"]
485+
print( # noqa: T201
486+
f"html2print: Chrome available at path: {path_to_chrome_browser}"
487+
)
488+
chrome_version = browser_info["version"]
489+
print( # noqa: T201
490+
f"html2print: Chrome version : {chrome_version}"
491+
)
492+
466493
path_to_chrome_driver = ChromeDriverManager().get_chrome_driver(
467-
path_to_cache_dir
494+
chrome_version=browser_info["version"],
495+
path_to_cache_dir=path_to_cache_dir,
468496
)
469497
print( # noqa: T201
470498
f"html2print: ChromeDriver available at path: {path_to_chrome_driver}"
471499
)
472-
473-
if browsers.get("chrome") is not None:
474-
path_to_chrome_browser = browsers.get("chrome")["path"]
475-
print( # noqa: T201
476-
f"html2print: Chrome available at path: {path_to_chrome_browser}"
477-
)
478500
sys.exit(0)
479501

480502
elif args.command == "print":
@@ -486,6 +508,7 @@ def main():
486508
args.cache_dir if args.cache_dir is not None else DEFAULT_CACHE_DIR
487509
)
488510
driver = create_webdriver(
511+
browser_info,
489512
args.chromedriver,
490513
path_to_cache_dir,
491514
page_load_timeout,

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ classifiers = [
5050
dependencies = [
5151
# HTML2PDF dependencies
5252
"selenium",
53-
"webdriver-manager",
5453

5554
# requests is used by HTML2PDF_HTTPClient.
5655
"requests",

0 commit comments

Comments
 (0)