Skip to content

Commit 36985d4

Browse files
authored
Merge pull request #62 from hhslepicka/load_strategy
ENH: Expose Page Load Strategy.
2 parents 480cbe6 + 39b1642 commit 36985d4

File tree

9 files changed

+92
-11
lines changed

9 files changed

+92
-11
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:

docs/browsers.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Here is an example on how to do that:
99
```python
1010
from botcity.web import WebBot, Browser
1111

12+
# Page Load Strategy
13+
from botcity.web import PageLoadStrategy
14+
1215
# For Chrome
1316
from botcity.web.browsers.chrome import default_options, default_capabilities
1417
# For Firefox
@@ -24,13 +27,16 @@ class Bot(WebBot):
2427
# Configure whether or not to run on headless mode
2528
self.headless = False
2629

30+
self.page_load_strategy = PageLoadStrategy.NORMAL
31+
2732
# Fetch the default options for my preferred browser
2833
# Pass in the headless, download_folder_path and user_data_dir
2934
# to be used when building the default_options
3035
def_options = default_options(
3136
headless=self.headless,
3237
download_folder_path=self.download_folder_path,
33-
user_data_dir=None # Informing None here will generate a temporary directory
38+
user_data_dir=None, # Informing None here will generate a temporary directory
39+
page_load_strategy=self.page_load_strategy
3440
)
3541

3642
# Add your customized argument
@@ -51,6 +57,14 @@ class Bot(WebBot):
5157
...
5258
```
5359

60+
## Page Load Strategy
61+
62+
Page Load Strategy is a browser option that determines how the browser will load the page.
63+
64+
::: botcity.web.browsers.PageLoadStrategy
65+
rendering:
66+
heading_level: 4
67+
5468
## Specific Browser Modules
5569

5670
Here are the documentation for the methods mentioned above for each of the supported browsers.
@@ -94,4 +108,4 @@ If you have any questions about the driver see [IE Driver Server Documentation](
94108

95109
See the [list of supported arguments in IE](https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/hh826025(v=vs.85)).
96110

97-
During execution some errors may occur, [see the list of common errors](https://testguild.com/selenium-webdriver-fix-for-3-common-ie-errors/).
111+
During execution some errors may occur, [see the list of common errors](https://testguild.com/selenium-webdriver-fix-for-3-common-ie-errors/).

docs/intro.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ proper configuration to launch the browser in the desired mode.
111111
and things such as cookies and stored passwords or certificates from one execution won't interfere with
112112
the others.
113113

114+
- **Page Load Strategy**: By default we use the `NORMAL` strategy which waits for the page to load completely.
115+
116+
114117
A handful of other options are also set and they can be inspected on the source code for each browser on the
115118
`botcity.web.browsers` module.
116119

@@ -120,7 +123,7 @@ the `default options` curated by BotCity and make your changes or start your opt
120123
In the following snippet we will cover how to build on top of the existing options.
121124

122125
```python
123-
from botcity.web import WebBot, Browser
126+
from botcity.web import WebBot, Browser, PageLoadStrategy
124127

125128
# For Chrome
126129
from botcity.web.browsers.chrome import default_options
@@ -138,7 +141,8 @@ class Bot(WebBot):
138141
def_options = default_options(
139142
headless=self.headless,
140143
download_folder_path=self.download_folder_path,
141-
user_data_dir=None # Informing None here will generate a temporary directory
144+
user_data_dir=None, # Informing None here will generate a temporary directory
145+
page_load_strategy=PageLoadStrategy.NORMAL
142146
)
143147

144148
# Add your customized argument

0 commit comments

Comments
 (0)