Repository navigation
Expand file tree
/
Copy pathautowait.py
More file actions
147 lines (119 loc) · 5.28 KB
/
Copy pathautowait.py
File metadata and controls
147 lines (119 loc) · 5.28 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
139
140
141
142
143
144
145
146
147
# Monkey-patching WebElement methods to add waiting logic for unclickable elements.
# This patch is applied globally to all WebElement instances.
import logging
from typing import Optional
from selenium.common import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from tools.ai_tools import lookup_better_selector, find_nearest_find_element_call
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
def patched_click_factory(timeout: float):
"""
Args:
timeout (float): Maximum time to wait for the element to become clickable.
"""
def patched_click(self):
"""
Patches the WebElement's click method to wait for the element to be clickable.
Args:
self: WebElement object.
"""
driver = self.parent
WebDriverWait(driver, timeout).until(EC.element_to_be_clickable(self))
self._original_click()
return patched_click
def patched_send_keys_factory(timeout: float):
"""
Args:
timeout (float): Maximum time to wait for the element to become clickable.
"""
def patched_send_keys(self, *args, **kwargs):
"""
Patches the WebElement's send_keys method to wait for the element to be clickable.
Args:
self: WebElement object.
"""
driver = self.parent
WebDriverWait(driver, timeout).until(EC.element_to_be_clickable(self))
self._original_send_keys(*args, **kwargs)
return patched_send_keys
def patched_clear_factory(timeout: float):
"""
Args:
timeout (float): Maximum time to wait for the element to become clickable.
"""
def patched_clear(self, *args, **kwargs):
"""
Patches the WebElement's clear method to wait for the element to be clickable.
Args:
self: WebElement object.
"""
driver = self.parent
WebDriverWait(driver, timeout).until(EC.element_to_be_clickable(self))
self._original_clear(*args, **kwargs)
return patched_clear
def patched_find_element_factory(timeout: float):
"""
Args:
timeout (float): Maximum time to wait for the element to be found.
"""
def patched_find_element(self, by=By.ID, value: Optional[str] = None) -> WebElement:
# Here we can't
try:
return WebDriverWait(self, timeout).until(
lambda driver: driver._original_find_element(by, value)
)
except TimeoutException as e:
stack_info = find_nearest_find_element_call()
logger.warning(
f'Can\'t find element by ("{by}", "{value}") in {stack_info.filename}:{stack_info.line_number}: {stack_info.code_line}'
)
logger.warning("Trying to look-up better alternative...")
better_by, better_value = lookup_better_selector(
by, value, self.page_source
)
logger.warning(
f'Found alternative: ("{better_by}", "{better_value}"). Re-trying to find the element...'
)
return self._original_find_element(better_by, better_value)
return patched_find_element
def enable_autowait(timeout: float = 10.0) -> None:
"""
Enables auto-waiting globally for all WebDriver.click() invokcations
Args:
self: WebElement object.
timeout (float): Maximum time to wait for the element to become clickable.
"""
assert not hasattr(WebElement, "_original_click"), "Autowait already enabled."
assert not hasattr(WebElement, "_original_send_keys"), "Autowait already enabled."
assert not hasattr(WebElement, "_original_clear"), "Autowait already enabled."
assert not hasattr(WebDriver, "_original_find_element"), "Autowait already enabled."
WebElement._original_click = WebElement.click
WebElement.click = patched_click_factory(timeout)
WebElement._original_send_keys = WebElement.send_keys
WebElement.send_keys = patched_send_keys_factory(timeout)
WebElement._original_clear = WebElement.clear
WebElement.clear = patched_clear_factory(timeout)
WebDriver._original_find_element = WebDriver.find_element
WebDriver.find_element = patched_find_element_factory(timeout)
def disable_autowait() -> None:
assert hasattr(WebElement, "_original_click"), "Autowait is not enabled."
assert hasattr(WebElement, "_original_send_keys"), "Autowait is not enabled."
assert hasattr(WebElement, "_original_clear"), "Autowait is not enabled."
assert hasattr(WebDriver, "_original_find_element"), "Autowait is not enabled."
WebElement.click = WebElement._original_click
del WebElement._original_click
WebElement.send_keys = WebElement._original_send_keys
del WebElement._original_send_keys
WebElement.clear = WebElement._original_clear
del WebElement._original_clear
WebDriver.find_element = WebDriver._original_find_element
del WebDriver._original_find_element