-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
selenium_helpers.py
86 lines (71 loc) · 2.81 KB
/
selenium_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations
import logging
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
logger = logging.getLogger(__name__)
def wait_until_notebook_loaded(driver: WebDriver, timeout: int = 30):
"""Wait until the Jupyter Notebook is loaded."""
try:
WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.ID, "notebook-container"))
)
except TimeoutException:
logger.exception("Timed out waiting for page to load")
driver.quit()
try:
WebDriverWait(driver, timeout).until(
# Sometimes if kernel is null, it will hang, so we check that.
lambda d: d.execute_script("return Jupyter.notebook.kernel == null")
is False
)
except TimeoutException:
logger.exception("Timed out waiting for kernel to load (null)")
driver.quit()
try:
WebDriverWait(driver, timeout).until(
# Sometimes if kernel is null, it will hang, so we check that.
lambda d: d.execute_script("return Jupyter.notebook.kernel.is_connected()")
is True
)
except TimeoutException:
logger.exception("Timed out waiting for kernel to load")
driver.quit()
def wait_until_notebook_list_loaded(driver: WebDriver, timeout: int = 10):
"""Wait until the Jupyter Notebook home page (list of files) is loaded."""
try:
WebDriverWait(driver, timeout).until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "#notebook_list > div > div > a > span")
)
)
except TimeoutException:
logger.exception("Timed out waiting for page to load")
driver.quit()
def wait_until_loaded(driver: WebDriver, timeout: int = 10):
"""Wait until the page is ready."""
try:
WebDriverWait(driver, timeout).until(
lambda d: d.execute_script("return document.readyState") == "complete"
)
except TimeoutException:
logger.exception("Timed out waiting for page to load")
driver.quit()
def wait_until_new_window(
driver: WebDriver, current_handles: list[str], timeout: int = 10
):
"""Wait until the page is ready."""
try:
WebDriverWait(driver, timeout).until(EC.new_window_is_opened(current_handles))
except TimeoutException:
logger.exception("Timed out waiting for a new window to open")
driver.quit()
def is_browser_disconnected(driver: WebDriver):
"""Check if the browser is disconnected."""
try:
_ = driver.window_handles
return False
except Exception:
return True