Skip to content

Commit 9467660

Browse files
Merge pull request #86 from kayqueGovetri/ENH/binary_path
ENH: Implement binary path
2 parents 4074f66 + a3267f5 commit 9467660

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed

botcity/web/bot.py

Lines changed: 24 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,27 @@ 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+
path = pathlib.Path(binary_path)
254+
if not path.is_file():
255+
raise ValueError("There is no file in the binary path.")
256+
self._binary_path = path
257+
235258
def start_browser(self):
236259
"""
237260
Starts the selected browser.
@@ -253,7 +276,7 @@ def check_driver():
253276
func_def_capabilities = BROWSER_CONFIGS.get(self.browser).get("capabilities")
254277

255278
opt = self.options or func_def_options(
256-
self.headless, self._download_folder_path, None, self.page_load_strategy
279+
self.headless, self._download_folder_path, None, self.page_load_strategy, self._binary_path
257280
)
258281
cap = self.capabilities or func_def_capabilities()
259282
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
@@ -17,7 +17,7 @@
1717

1818

1919
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
20-
page_load_strategy="normal") -> ChromeOptions:
20+
page_load_strategy="normal", binary_path: str = None) -> ChromeOptions:
2121
"""Retrieve the default options for this browser curated by BotCity.
2222
2323
Args:
@@ -27,7 +27,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
2727
user_data_dir ([type], optional): The directory to use as user profile.
2828
If None, a new temporary directory is used. Defaults to None.
2929
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".
30-
30+
binary_path (str, optional): The path to the browser binary.
3131
Returns:
3232
ChromeOptions: The Chrome options.
3333
"""
@@ -36,6 +36,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
3636
page_load_strategy = page_load_strategy.value
3737
except AttributeError:
3838
page_load_strategy = page_load_strategy
39+
if binary_path:
40+
chrome_options.binary_location = str(binary_path)
3941
chrome_options.page_load_strategy = page_load_strategy
4042
chrome_options.add_argument("--remote-debugging-port=0")
4143
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)