Skip to content

Commit c110ac6

Browse files
committed
ENH: Expose Page Load Strategy.
1 parent e34c6b8 commit c110ac6

File tree

7 files changed

+70
-7
lines changed

7 files changed

+70
-7
lines changed

botcity/web/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .bot import WebBot, Browser, BROWSER_CONFIGS, By # noqa: F401, F403
1+
from .bot import WebBot, Browser, BROWSER_CONFIGS, By, PageLoadStrategy # noqa: F401, F403
22
from .parsers import table_to_dict, data_from_row, sanitize_header # noqa: F401, F403
33
from .util import element_as_select # noqa: F401, F403
44

botcity/web/bot.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from selenium.webdriver.support import expected_conditions as EC
2727

2828
from . import config, cv2find
29-
from .browsers import BROWSER_CONFIGS, Browser
29+
from .browsers import BROWSER_CONFIGS, Browser, PageLoadStrategy
3030

3131
try:
3232
from botcity.maestro import BotMaestroSDK
@@ -62,6 +62,7 @@ def __init__(self, headless=False):
6262

6363
self._driver = None
6464
self._headless = headless
65+
self._page_load_strategy = PageLoadStrategy.NORMAL
6566

6667
self._clipboard = ""
6768

@@ -201,6 +202,28 @@ def headless(self, headless):
201202
logger.warning("Browser is running. Invoke stop_browser and start browser for changes to take effect.")
202203
self._headless = headless
203204

205+
@property
206+
def page_load_strategy(self) -> PageLoadStrategy:
207+
"""
208+
The page load strategy to be used.
209+
210+
Returns:
211+
page_load_strategy (PageLoadStrategy): The page load strategy to be used.
212+
"""
213+
return self._page_load_strategy
214+
215+
@page_load_strategy.setter
216+
def page_load_strategy(self, page_load_strategy: PageLoadStrategy):
217+
"""
218+
The page load strategy to be used.
219+
220+
Args:
221+
page_load_strategy (PageLoadStrategy): The page load strategy to be used.
222+
"""
223+
if self._driver:
224+
logger.warning("Browser is running. Invoke stop_browser and start browser for changes to take effect.")
225+
self._page_load_strategy = page_load_strategy
226+
204227
def start_browser(self):
205228
"""
206229
Starts the selected browser.
@@ -222,7 +245,9 @@ def check_driver():
222245
# Specific capabilities method for a given browser
223246
func_def_capabilities = BROWSER_CONFIGS.get(self.browser).get("capabilities")
224247

225-
opt = self.options or func_def_options(self.headless, self._download_folder_path, None)
248+
opt = self.options or func_def_options(
249+
self.headless, self._download_folder_path, None, self.page_load_strategy
250+
)
226251
cap = self.capabilities or func_def_capabilities()
227252
self.options = opt
228253
self.capabilities = cap

botcity/web/browsers/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ class Browser(str, enum.Enum):
2222
IE = "ie"
2323

2424

25+
class PageLoadStrategy(str, enum.Enum):
26+
"""
27+
Page Load Strategy.
28+
29+
Attributes:
30+
NORMAL (str): Wait for the entire page is loaded. When set to normal,
31+
waits until the load event fire is returned.
32+
EAGER (str): Wait until the initial HTML document has been completely
33+
loaded and parsed, and discards loading of stylesheets, images and subframes.
34+
NONE (str): Only waits until the initial page is downloaded
35+
"""
36+
NORMAL = "normal"
37+
EAGER = "eager"
38+
NONE = "none"
39+
40+
2541
BROWSER_CONFIGS = {
2642
Browser.CHROME: {
2743
"driver": "chromedriver",

botcity/web/browsers/chrome.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from ..util import cleanup_temp_dir
1212

1313

14-
def default_options(headless=False, download_folder_path=None, user_data_dir=None) -> ChromeOptions:
14+
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
15+
page_load_strategy="normal") -> ChromeOptions:
1516
"""Retrieve the default options for this browser curated by BotCity.
1617
1718
Args:
@@ -20,11 +21,17 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
2021
If None, the current directory is used. Defaults to None.
2122
user_data_dir ([type], optional): The directory to use as user profile.
2223
If None, a new temporary directory is used. Defaults to None.
24+
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".
2325
2426
Returns:
2527
ChromeOptions: The Chrome options.
2628
"""
2729
chrome_options = ChromeOptions()
30+
try:
31+
page_load_strategy = page_load_strategy.value
32+
except AttributeError:
33+
page_load_strategy = page_load_strategy
34+
chrome_options.page_load_strategy = page_load_strategy
2835
chrome_options.add_argument("--remote-debugging-port=0")
2936
chrome_options.add_argument("--no-first-run")
3037
chrome_options.add_argument("--no-default-browser-check")

botcity/web/browsers/edge.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from ..util import cleanup_temp_dir
1212

1313

14-
def default_options(headless=False, download_folder_path=None, user_data_dir=None) -> EdgeOptions:
14+
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
15+
page_load_strategy="normal") -> EdgeOptions:
1516
"""Retrieve the default options for this browser curated by BotCity.
1617
1718
Args:
@@ -20,11 +21,17 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
2021
If None, the current directory is used. Defaults to None.
2122
user_data_dir ([type], optional): The directory to use as user profile.
2223
If None, a new temporary directory is used. Defaults to None.
24+
page_load_strategy (str, optional): The page load strategy. Defaults to "normal".
2325
2426
Returns:
2527
EdgeOptions: The Edge options.
2628
"""
2729
edge_options = EdgeOptions()
30+
try:
31+
page_load_strategy = page_load_strategy.value
32+
except AttributeError:
33+
page_load_strategy = page_load_strategy
34+
edge_options.page_load_strategy = page_load_strategy
2835
edge_options.use_chromium = True
2936
edge_options.add_argument("--remote-debugging-port=0")
3037
edge_options.add_argument("--no-first-run")

botcity/web/browsers/firefox.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@
336336
'application/vnd.zzazz.deck+xml']
337337

338338

339-
def default_options(headless=False, download_folder_path=None, user_data_dir=None) -> FirefoxOptions:
339+
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
340+
page_load_strategy="normal") -> FirefoxOptions:
340341
"""Retrieve the default options for this browser curated by BotCity.
341342
342343
Args:
@@ -345,11 +346,17 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
345346
If None, the current directory is used. Defaults to None.
346347
user_data_dir ([type], optional): The directory to use as user profile.
347348
If None, a new temporary directory is used. Defaults to None.
349+
page_load_strategy (str, optional): The page load strategy to use.
348350
349351
Returns:
350352
FirefoxOptions: The Firefox options.
351353
"""
352354
firefox_options = FirefoxOptions()
355+
try:
356+
page_load_strategy = page_load_strategy.value
357+
except AttributeError:
358+
page_load_strategy = page_load_strategy
359+
firefox_options.page_load_strategy = page_load_strategy
353360
firefox_options.headless = headless
354361
if not user_data_dir:
355362
temp_dir = tempfile.TemporaryDirectory(prefix="botcity_")

botcity/web/browsers/ie.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from selenium.webdriver.ie.options import Options
66

77

8-
def default_options(headless=False, download_folder_path=None, user_data_dir=None) -> Options:
8+
def default_options(headless=False, download_folder_path=None, user_data_dir=None,
9+
page_load_strategy="normal") -> Options:
910
"""Retrieve the default options for this browser curated by BotCity.
1011
1112
Returns:

0 commit comments

Comments
 (0)