Skip to content

Commit f540ba4

Browse files
[py] Move loading generated JS into webelement into a method
1 parent 7f7d635 commit f540ba4

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

py/selenium/webdriver/remote/webelement.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@
3333

3434
# TODO: When moving to supporting python 3.9 as the minimum version we can
3535
# use built in importlib_resources.files.
36-
_pkg = '.'.join(__name__.split('.')[:-1])
37-
getAttribute_js = pkgutil.get_data(_pkg, 'getAttribute.js').decode('utf8')
38-
isDisplayed_js = pkgutil.get_data(_pkg, 'isDisplayed.js').decode('utf8')
36+
getAttribute_js = None
37+
isDisplayed_js = None
38+
39+
40+
def _load_js():
41+
global getAttribute_js
42+
global isDisplayed_js
43+
_pkg = '.'.join(__name__.split('.')[:-1])
44+
getAttribute_js = pkgutil.get_data(_pkg, 'getAttribute.js').decode('utf8')
45+
isDisplayed_js = pkgutil.get_data(_pkg, 'isDisplayed.js').decode('utf8')
3946

4047

4148
class BaseWebElement(metaclass=ABCMeta):
@@ -151,7 +158,8 @@ def get_attribute(self, name) -> str:
151158
is_active = "active" in target_element.get_attribute("class")
152159
153160
"""
154-
161+
if getAttribute_js is None:
162+
_load_js()
155163
attribute_value = self.parent.execute_script(
156164
"return (%s).apply(null, arguments);" % getAttribute_js,
157165
self, name)
@@ -591,6 +599,8 @@ def shadow_root(self) -> ShadowRoot:
591599
def is_displayed(self) -> bool:
592600
"""Whether the element is visible to a user."""
593601
# Only go into this conditional for browsers that don't use the atom themselves
602+
if isDisplayed_js is None:
603+
_load_js()
594604
return self.parent.execute_script(
595605
"return (%s).apply(null, arguments);" % isDisplayed_js,
596606
self)

0 commit comments

Comments
 (0)