Skip to content

Commit d51ed63

Browse files
committed
ENH: Implement binary path
1 parent 26376d7 commit d51ed63

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

botcity/web/bot.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import logging
77
import os
8+
import pathlib
89
import platform
910
import random
1011
import re
@@ -65,6 +66,7 @@ def __init__(self, headless=False):
6566
self._driver = None
6667
self._headless = headless
6768
self._page_load_strategy = PageLoadStrategy.NORMAL
69+
self._binary_path = None
6870

6971
self._clipboard = ""
7072

@@ -232,6 +234,24 @@ def page_load_strategy(self, page_load_strategy: PageLoadStrategy):
232234
logger.warning("Browser is running. Invoke stop_browser and start browser for changes to take effect.")
233235
self._page_load_strategy = page_load_strategy
234236

237+
@property
238+
def binary_path(self):
239+
"""The binary path to be used.
240+
241+
Returns:
242+
binary_path (pathlib.Path): The binary path to be used.
243+
"""
244+
return pathlib.Path(self._binary_path)
245+
246+
@binary_path.setter
247+
def binary_path(self, binary_path: str):
248+
"""The binary path to be used.
249+
250+
Args:
251+
binary_path (str): The binary path to be used.
252+
"""
253+
self._binary_path = pathlib.Path(binary_path)
254+
235255
def start_browser(self):
236256
"""
237257
Starts the selected browser.
@@ -253,7 +273,7 @@ def check_driver():
253273
func_def_capabilities = BROWSER_CONFIGS.get(self.browser).get("capabilities")
254274

255275
opt = self.options or func_def_options(
256-
self.headless, self._download_folder_path, None, self.page_load_strategy
276+
self.headless, self._download_folder_path, None, self.page_load_strategy, self._binary_path
257277
)
258278
cap = self.capabilities or func_def_capabilities()
259279
self.options = opt

botcity/web/browsers/chrome.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
15-
page_load_strategy="normal") -> ChromeOptions:
15+
page_load_strategy="normal", binary_path: str = None) -> ChromeOptions:
1616
"""Retrieve the default options for this browser curated by BotCity.
1717
1818
Args:
@@ -22,6 +22,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
2222
user_data_dir ([type], optional): The directory to use as user profile.
2323
If None, a new temporary directory is used. Defaults to None.
2424
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".
25+
binary_path (str, optional): The path to the browser binary.
2526
2627
Returns:
2728
ChromeOptions: The Chrome options.
@@ -31,6 +32,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
3132
page_load_strategy = page_load_strategy.value
3233
except AttributeError:
3334
page_load_strategy = page_load_strategy
35+
if binary_path:
36+
chrome_options.binary_location = str(binary_path)
3437
chrome_options.page_load_strategy = page_load_strategy
3538
chrome_options.add_argument("--remote-debugging-port=0")
3639
chrome_options.add_argument("--no-first-run")

botcity/web/browsers/edge.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
15-
page_load_strategy="normal") -> EdgeOptions:
15+
page_load_strategy="normal", binary_path: str = None) -> EdgeOptions:
1616
"""Retrieve the default options for this browser curated by BotCity.
1717
Args:
1818
headless (bool, optional): Whether or not to use the headless mode. Defaults to False.
@@ -21,6 +21,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
2121
user_data_dir ([type], optional): The directory to use as user profile.
2222
If None, a new temporary directory is used. Defaults to None.
2323
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".
24+
binary_path (str, optional): The path to the browser binary.
2425
Returns:
2526
EdgeOptions: The Edge options.
2627
"""
@@ -29,6 +30,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
2930
page_load_strategy = page_load_strategy.value
3031
except AttributeError:
3132
page_load_strategy = page_load_strategy
33+
if binary_path:
34+
edge_options.binary_location = str(binary_path)
3235
edge_options.page_load_strategy = page_load_strategy
3336
edge_options.use_chromium = True
3437
edge_options.add_argument("--remote-debugging-port=0")

botcity/web/browsers/firefox.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@
337337

338338

339339
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
340-
page_load_strategy="normal") -> FirefoxOptions:
340+
page_load_strategy="normal", binary_path: str = None) -> FirefoxOptions:
341341
"""Retrieve the default options for this browser curated by BotCity.
342342
343343
Args:
@@ -347,7 +347,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
347347
user_data_dir ([type], optional): The directory to use as user profile.
348348
If None, a new temporary directory is used. Defaults to None.
349349
page_load_strategy (str, optional): The page load strategy to use.
350-
350+
binary_path (str, optional): The path to the browser binary.
351351
Returns:
352352
FirefoxOptions: The Firefox options.
353353
"""
@@ -363,6 +363,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
363363
temp_dir = tempfile.TemporaryDirectory(prefix="botcity_")
364364
user_data_dir = temp_dir.name
365365
atexit.register(cleanup_temp_dir, temp_dir)
366+
if binary_path:
367+
firefox_options.binary_location = str(binary_path)
366368
firefox_options.set_preference("profile", user_data_dir)
367369
firefox_options.set_preference("security.default_personal_cert", "Select Automatically")
368370
firefox_options.set_preference('browser.download.folderList', 2)

botcity/web/browsers/undetected_chrome.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
19-
page_load_strategy="normal") -> ChromeOptions:
19+
page_load_strategy="normal", binary_path: str = None) -> ChromeOptions:
2020
"""Retrieve the default options for this browser curated by BotCity.
2121
2222
Args:
@@ -26,7 +26,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
2626
user_data_dir ([type], optional): The directory to use as user profile.
2727
If None, a new temporary directory is used. Defaults to None.
2828
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".
29-
29+
binary_path (str, optional): The path to the browser binary.
3030
Returns:
3131
ChromeOptions: The Chrome options.
3232
"""
@@ -35,6 +35,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
3535
page_load_strategy = page_load_strategy.value
3636
except AttributeError:
3737
page_load_strategy = page_load_strategy
38+
if binary_path:
39+
chrome_options.binary_location = str(binary_path)
3840
chrome_options.page_load_strategy = page_load_strategy
3941
chrome_options.add_argument("--remote-debugging-port=0")
4042
chrome_options.add_argument("--no-first-run")

conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def setup_firefox(headless: bool, tmp_folder: str, download_driver: str) -> WebB
5454

5555
web.driver_path = download_driver
5656
web.download_folder_path = tmp_folder
57-
5857
return web
5958

6059

0 commit comments

Comments
 (0)