-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from smkent/chrome-driver
Include undetected_chromedriver binary in Docker image
- Loading branch information
Showing
4 changed files
with
69 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import contextlib | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
from typing import Iterator | ||
|
||
import undetected_chromedriver as uc # type: ignore | ||
|
||
CHROMEDRIVER_PATH = ( | ||
Path.home() | ||
/ ".local" | ||
/ "share" | ||
/ "undetected_chromedriver" | ||
/ "undetected_chromedriver" | ||
) | ||
|
||
|
||
class ChromeDriverDoesNotExist(Exception): | ||
pass | ||
|
||
|
||
@contextlib.contextmanager | ||
def chrome_driver(headless: bool = True) -> Iterator[uc.Chrome]: | ||
options = uc.ChromeOptions() | ||
options.headless = headless | ||
for option in [ | ||
"--incognito", | ||
"--no-sandbox", | ||
"--disable-extensions", | ||
"--disable-application-cache", | ||
"--disable-gpu", | ||
"--disable-setuid-sandbox", | ||
"--disable-dev-shm-usage", | ||
]: | ||
options.add_argument(option) | ||
if headless: | ||
options.add_argument("--headless=new") | ||
driver = uc.Chrome(options=options) | ||
yield driver | ||
driver.quit() | ||
|
||
|
||
def chrome_driver_version() -> str: | ||
if not CHROMEDRIVER_PATH.is_file(): | ||
raise ChromeDriverDoesNotExist( | ||
f"Error: {CHROMEDRIVER_PATH} does not exist" | ||
) | ||
cmd = [str(CHROMEDRIVER_PATH), "--version"] | ||
print(f"+ {' '.join(cmd)}", file=sys.stderr) | ||
result = subprocess.run(cmd, capture_output=True) | ||
return result.stdout.decode() | ||
|
||
|
||
def init() -> None: | ||
with contextlib.suppress(ChromeDriverDoesNotExist): | ||
print(chrome_driver_version()) | ||
return | ||
print("Initializing Chrome Driver") | ||
with chrome_driver() as driver: | ||
print("Connect to example.com") | ||
driver.get("https://example.com") | ||
print(chrome_driver_version()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters