Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion botcity/web/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import os
import pathlib
import platform
import random
import re
Expand Down Expand Up @@ -65,6 +66,7 @@ def __init__(self, headless=False):
self._driver = None
self._headless = headless
self._page_load_strategy = PageLoadStrategy.NORMAL
self._binary_path = None

self._clipboard = ""

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

@property
def binary_path(self):
"""The binary path to be used.

Returns:
binary_path (pathlib.Path): The binary path to be used.
"""
return pathlib.Path(self._binary_path)

@binary_path.setter
def binary_path(self, binary_path: str):
"""The binary path to be used.

Args:
binary_path (str): The binary path to be used.
"""
self._binary_path = pathlib.Path(binary_path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to validate the binary_path parameter to ensure that it exists and is a valid file before assigning it.


def start_browser(self):
"""
Starts the selected browser.
Expand All @@ -253,7 +273,7 @@ def check_driver():
func_def_capabilities = BROWSER_CONFIGS.get(self.browser).get("capabilities")

opt = self.options or func_def_options(
self.headless, self._download_folder_path, None, self.page_load_strategy
self.headless, self._download_folder_path, None, self.page_load_strategy, self._binary_path
)
cap = self.capabilities or func_def_capabilities()
self.options = opt
Expand Down
5 changes: 4 additions & 1 deletion botcity/web/browsers/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def default_options(headless=False, download_folder_path=None, user_data_dir=None,
page_load_strategy="normal") -> ChromeOptions:
page_load_strategy="normal", binary_path: str = None) -> ChromeOptions:
"""Retrieve the default options for this browser curated by BotCity.

Args:
Expand All @@ -22,6 +22,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
user_data_dir ([type], optional): The directory to use as user profile.
If None, a new temporary directory is used. Defaults to None.
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".
binary_path (str, optional): The path to the browser binary.

Returns:
ChromeOptions: The Chrome options.
Expand All @@ -31,6 +32,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
page_load_strategy = page_load_strategy.value
except AttributeError:
page_load_strategy = page_load_strategy
if binary_path:
chrome_options.binary_location = str(binary_path)
chrome_options.page_load_strategy = page_load_strategy
chrome_options.add_argument("--remote-debugging-port=0")
chrome_options.add_argument("--no-first-run")
Expand Down
5 changes: 4 additions & 1 deletion botcity/web/browsers/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def default_options(headless=False, download_folder_path=None, user_data_dir=None,
page_load_strategy="normal") -> EdgeOptions:
page_load_strategy="normal", binary_path: str = None) -> EdgeOptions:
"""Retrieve the default options for this browser curated by BotCity.
Args:
headless (bool, optional): Whether or not to use the headless mode. Defaults to False.
Expand All @@ -21,6 +21,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
user_data_dir ([type], optional): The directory to use as user profile.
If None, a new temporary directory is used. Defaults to None.
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".
binary_path (str, optional): The path to the browser binary.
Returns:
EdgeOptions: The Edge options.
"""
Expand All @@ -29,6 +30,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
page_load_strategy = page_load_strategy.value
except AttributeError:
page_load_strategy = page_load_strategy
if binary_path:
edge_options.binary_location = str(binary_path)
edge_options.page_load_strategy = page_load_strategy
edge_options.use_chromium = True
edge_options.add_argument("--remote-debugging-port=0")
Expand Down
6 changes: 4 additions & 2 deletions botcity/web/browsers/firefox.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@


def default_options(headless=False, download_folder_path=None, user_data_dir=None,
page_load_strategy="normal") -> FirefoxOptions:
page_load_strategy="normal", binary_path: str = None) -> FirefoxOptions:
"""Retrieve the default options for this browser curated by BotCity.

Args:
Expand All @@ -347,7 +347,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
user_data_dir ([type], optional): The directory to use as user profile.
If None, a new temporary directory is used. Defaults to None.
page_load_strategy (str, optional): The page load strategy to use.

binary_path (str, optional): The path to the browser binary.
Returns:
FirefoxOptions: The Firefox options.
"""
Expand All @@ -363,6 +363,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
temp_dir = tempfile.TemporaryDirectory(prefix="botcity_")
user_data_dir = temp_dir.name
atexit.register(cleanup_temp_dir, temp_dir)
if binary_path:
firefox_options.binary_location = str(binary_path)
firefox_options.set_preference("profile", user_data_dir)
firefox_options.set_preference("security.default_personal_cert", "Select Automatically")
firefox_options.set_preference('browser.download.folderList', 2)
Expand Down
6 changes: 4 additions & 2 deletions botcity/web/browsers/undetected_chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def default_options(headless=False, download_folder_path=None, user_data_dir=None,
page_load_strategy="normal") -> ChromeOptions:
page_load_strategy="normal", binary_path: str = None) -> ChromeOptions:
"""Retrieve the default options for this browser curated by BotCity.

Args:
Expand All @@ -26,7 +26,7 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
user_data_dir ([type], optional): The directory to use as user profile.
If None, a new temporary directory is used. Defaults to None.
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".

binary_path (str, optional): The path to the browser binary.
Returns:
ChromeOptions: The Chrome options.
"""
Expand All @@ -35,6 +35,8 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
page_load_strategy = page_load_strategy.value
except AttributeError:
page_load_strategy = page_load_strategy
if binary_path:
chrome_options.binary_location = str(binary_path)
chrome_options.page_load_strategy = page_load_strategy
chrome_options.add_argument("--remote-debugging-port=0")
chrome_options.add_argument("--no-first-run")
Expand Down
1 change: 0 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def setup_firefox(headless: bool, tmp_folder: str, download_driver: str) -> WebB

web.driver_path = download_driver
web.download_folder_path = tmp_folder

return web


Expand Down