(20260317) Tried headless and parallel mode.
This commit is contained in:
+32
-7
@@ -105,6 +105,9 @@ class CosecWeb:
|
||||
driver_dir: str,
|
||||
user_data_dir: str,
|
||||
downloads_dir: str,
|
||||
window_width: int = 1920,
|
||||
window_height: int = 1080,
|
||||
headless: bool = True,
|
||||
debug: bool = True,
|
||||
debug_prefix: str = "Cosec-Web | "
|
||||
):
|
||||
@@ -117,6 +120,9 @@ class CosecWeb:
|
||||
:param driver_dir: The directory where the driver is placed.
|
||||
:param user_data_dir: The directory where you want the web-driver to store user data.
|
||||
:param downloads_dir: The directory where you want to hold the downloaded files.
|
||||
:param window_width: The width of the browser window.
|
||||
:param window_height: The height of the browser window.
|
||||
:param headless: Whether, or not, to use Chrome browser in headless (No UI) mode.
|
||||
:param debug: Whether, or not, to show debug messages.
|
||||
:param debug_prefix: The prefix to show before the debug messages.
|
||||
"""
|
||||
@@ -135,6 +141,9 @@ class CosecWeb:
|
||||
self.driver_dir = driver_dir
|
||||
self.user_data_dir = user_data_dir
|
||||
self.downloads_dir = downloads_dir
|
||||
self.headless = headless
|
||||
self.window_width = window_width
|
||||
self.window_height = window_height
|
||||
|
||||
# Figure out which driver to use:
|
||||
self.driver_path = None
|
||||
@@ -148,7 +157,7 @@ class CosecWeb:
|
||||
# If we don't have a matching driver:
|
||||
if self.driver_path is None: raise NotImplementedError("OS and/or CPU Architecture Not Supported.")
|
||||
|
||||
# Else, we initialize the driver:
|
||||
# Else, we start constructing the preferred configuration::
|
||||
self._nc_printer("Using driver:", self.driver_path)
|
||||
self._service = Service(self.driver_path)
|
||||
self._options = webdriver.ChromeOptions()
|
||||
@@ -158,26 +167,42 @@ class CosecWeb:
|
||||
self._options.add_experimental_option(
|
||||
"prefs",
|
||||
{
|
||||
"downloads.default_directory": downloads_dir, # ... The custom downloads path.
|
||||
"downloads.prompt_for_download": False, # ......... Do not show save dialog.
|
||||
"downloads.directory_upgrade": True, # ............ If path doesn't exist, try to create it.
|
||||
"safebrowsing.enabled": True, # .................. Allow safe downloads.
|
||||
"downloads.default_directory": downloads_dir, # ....... The custom 'downloads' path.
|
||||
"downloads.prompt_for_download": False, # ............. Do not show save dialog.
|
||||
"downloads.directory_upgrade": True, # ................ If path doesn't exist, try to create it.
|
||||
"safebrowsing.enabled": True, # ....................... Allow safe downloads.
|
||||
"safebrowsing.disable_download_protection": True, # ... Don't show "keep" button before downloading.
|
||||
"profile.default_content_settings.popups": 0,
|
||||
"profile.default_content_setting_values.automatic_downloads": 1,
|
||||
"profile.content_settings.exceptions.automatic_downloads.*.setting": 1,
|
||||
}
|
||||
)
|
||||
|
||||
# For headless mode:
|
||||
if headless:
|
||||
self._printer("HEADLESS MODE!")
|
||||
self._options.add_argument("--headless=new") # .................................. Modern headless mode.
|
||||
self._options.add_argument("--disable-gpu") # ................................... Optional (mostly for Windows).
|
||||
self._options.add_argument(f"--window-size={window_width},{window_height}") # ... The internally simulated screen size.
|
||||
|
||||
# Initialize the driver:
|
||||
self._driver = webdriver.Chrome(service = self._service, options = self._options)
|
||||
self._driver.set_page_load_timeout(300)
|
||||
self._driver.execute_cdp_cmd("Page.setDownloadBehavior", {
|
||||
"behavior": "allow",
|
||||
"downloadPath": downloads_dir
|
||||
})
|
||||
self._printer("Driver ready.")
|
||||
|
||||
# For later use, we will need variables to hold window information:
|
||||
self._original_window = None
|
||||
self._current_window = None
|
||||
|
||||
def __del__(self):
|
||||
self._printer("Deinitializing!")
|
||||
try: self.quit()
|
||||
except Exception as exception: self._printer(exception)
|
||||
|
||||
# ┳┓ ┓ •
|
||||
# ┃┃┏┓┣┓┓┏┏┓┏┓┓┏┓┏┓
|
||||
# ┻┛┗ ┗┛┗┻┗┫┗┫┗┛┗┗┫
|
||||
@@ -748,8 +773,8 @@ class CosecWeb:
|
||||
self._driver.switch_to.window(self._current_window)
|
||||
self._printer("Context switched.", self._current_window)
|
||||
|
||||
# Make the window full-screen for standardized behaviour:
|
||||
self._driver.maximize_window()
|
||||
# Set the size of the window so that you are certain that all the elements will be visible:
|
||||
self._driver.set_window_size(self.window_width, self.window_height)
|
||||
|
||||
# The username text field:
|
||||
element = wait.until(EC.presence_of_element_located((By.ID, "loginid")))
|
||||
|
||||
Reference in New Issue
Block a user