Selenium Infrastructure
Utilities for managing WebDriver connections, Dagster resources, and browser interactions specific to the Scraper module.
Lifecycle Management
Tools to create, validate, and destroy WebDriver sessions connecting to the Selenium Grid.
Perhaps Selenium could be placed in the
utilsfolder, and not thesourcesfolder. Why? Here it's only present in thesourcesfolder, but Selenium could be used in automated tests in the future. I'll leave that to my future self.
drugslm.utils.selenium
Selenium WebDriver Utilities
This module provides utilities for managing Selenium WebDriver connections, specifically for remote drivers connected to a Selenium Hub.
It includes: - A context manager 'webdriver_manager' for safe setup/teardown. - A 'validate_driver_connection' health check function. - Helper functions for UI interaction and configuration loading.
get_firefox_options()
Loads Firefox configurations from the config object and returns the options.
Returns:
| Name | Type | Description |
|---|---|---|
FirefoxOptions |
Options
|
A configured options object ready for the GeckoDriver, populated with arguments and preferences from the YAML/JSON file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the config object is None or the underlying file is missing. |
ValueError
|
If the file format is unsupported (handled by .load()). |
Source code in drugslm/utils/selenium.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
highlight(driver, element, color='green')
Highlights a specific WebElement by drawing a border around it using JS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
driver
|
WebDriver
|
The active Selenium WebDriver instance. |
required |
element
|
WebElement
|
The target Selenium WebElement instance. |
required |
color
|
str
|
The border color. Defaults to "green". |
'green'
|
Source code in drugslm/utils/selenium.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
scroll(driver, element)
Scrolls the page to bring the specified WebElement into view using JS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
driver
|
WebDriver
|
The active Selenium WebDriver instance. |
required |
element
|
WebElement
|
The target Selenium WebElement instance. |
required |
Source code in drugslm/utils/selenium.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
validate_driver_connection(driver)
Performs a basic health check on an active WebDriver instance.
It attempts to load a known page (google.com) and verifies that basic functionality (JS execution, element finding) is working.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
driver
|
WebDriver
|
The WebDriver instance to test. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the validation is successful. |
Raises:
| Type | Description |
|---|---|
Exception
|
If any assertion or step in the validation fails. |
Source code in drugslm/utils/selenium.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
webdriver_manager(hub_url=DEFAULT_HUB_URL, browser='firefox')
Manages the life cycle of a remote WebDriver as a context manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hub_url
|
str
|
The URL of the Selenium Hub. Defaults to DEFAULT_HUB_URL. |
DEFAULT_HUB_URL
|
browser
|
Literal['firefox', 'chrome']
|
The browser name to be used. Default "firefox". |
'firefox'
|
Yields:
| Name | Type | Description |
|---|---|---|
WebDriver |
WebDriver
|
A live, validated Selenium WebDriver instance. |
Raises:
| Type | Description |
|---|---|
Exception
|
Propagates any exception during connection, validation, or if the 'retry' attempts fail. |
Source code in drugslm/utils/selenium.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |