@@ -5895,7 +5895,9 @@ def skip(self, reason=""):
5895
5895
5896
5896
# Shadow DOM / Shadow-root methods
5897
5897
5898
- def __get_shadow_element(self, selector, timeout=None):
5898
+ def __get_shadow_element(
5899
+ self, selector, timeout=None, must_be_visible=False
5900
+ ):
5899
5901
self.wait_for_ready_state_complete()
5900
5902
if timeout is None:
5901
5903
timeout = settings.SMALL_TIMEOUT
@@ -5911,6 +5913,7 @@ def __get_shadow_element(self, selector, timeout=None):
5911
5913
selectors = selector.split("::shadow ")
5912
5914
element = self.get_element(selectors[0])
5913
5915
selector_chain = selectors[0]
5916
+ is_present = False
5914
5917
for selector_part in selectors[1:]:
5915
5918
shadow_root = None
5916
5919
if (
@@ -5997,6 +6000,11 @@ def __get_shadow_element(self, selector, timeout=None):
5997
6000
try:
5998
6001
element = shadow_root.find_element(
5999
6002
By.CSS_SELECTOR, value=selector_part)
6003
+ is_present = True
6004
+ if must_be_visible:
6005
+ if not element.is_displayed():
6006
+ raise Exception(
6007
+ "Shadow Root element not visible!")
6000
6008
found = True
6001
6009
break
6002
6010
except Exception:
@@ -6005,6 +6013,10 @@ def __get_shadow_element(self, selector, timeout=None):
6005
6013
if not found:
6006
6014
element = shadow_root.find_element(
6007
6015
By.CSS_SELECTOR, value=selector_part)
6016
+ is_present = True
6017
+ if must_be_visible and not element.is_displayed():
6018
+ raise Exception(
6019
+ "Shadow Root element not visible!")
6008
6020
else:
6009
6021
element = page_actions.wait_for_element_present(
6010
6022
shadow_root,
@@ -6013,11 +6025,16 @@ def __get_shadow_element(self, selector, timeout=None):
6013
6025
timeout=timeout,
6014
6026
)
6015
6027
except Exception:
6028
+ error = "not present"
6029
+ the_exception = "NoSuchElementException"
6030
+ if must_be_visible and is_present:
6031
+ error = "not visible"
6032
+ the_exception = "ElementNotVisibleException"
6016
6033
msg = (
6017
- "Shadow DOM Element {%s} was not present after %s seconds!"
6018
- % (selector_chain, timeout)
6034
+ "Shadow DOM Element {%s} was %s after %s seconds!"
6035
+ % (selector_chain, error, timeout)
6019
6036
)
6020
- page_actions.timeout_exception("NoSuchElementException" , msg)
6037
+ page_actions.timeout_exception(the_exception , msg)
6021
6038
return element
6022
6039
6023
6040
def __fail_if_invalid_shadow_selector_usage(self, selector):
@@ -6035,11 +6052,15 @@ def __is_shadow_selector(self, selector):
6035
6052
return False
6036
6053
6037
6054
def __shadow_click(self, selector, timeout):
6038
- element = self.__get_shadow_element(selector, timeout=timeout)
6055
+ element = self.__get_shadow_element(
6056
+ selector, timeout=timeout, must_be_visible=True
6057
+ )
6039
6058
element.click()
6040
6059
6041
6060
def __shadow_type(self, selector, text, timeout, clear_first=True):
6042
- element = self.__get_shadow_element(selector, timeout=timeout)
6061
+ element = self.__get_shadow_element(
6062
+ selector, timeout=timeout, must_be_visible=True
6063
+ )
6043
6064
if clear_first:
6044
6065
try:
6045
6066
element.clear()
@@ -6060,7 +6081,9 @@ def __shadow_type(self, selector, text, timeout, clear_first=True):
6060
6081
self.wait_for_ready_state_complete()
6061
6082
6062
6083
def __shadow_clear(self, selector, timeout):
6063
- element = self.__get_shadow_element(selector, timeout=timeout)
6084
+ element = self.__get_shadow_element(
6085
+ selector, timeout=timeout, must_be_visible=True
6086
+ )
6064
6087
try:
6065
6088
element.clear()
6066
6089
backspaces = Keys.BACK_SPACE * 42 # Autofill Defense
@@ -6069,8 +6092,13 @@ def __shadow_clear(self, selector, timeout):
6069
6092
pass
6070
6093
6071
6094
def __get_shadow_text(self, selector, timeout):
6072
- element = self.__get_shadow_element(selector, timeout=timeout)
6073
- return element.text
6095
+ element = self.__get_shadow_element(
6096
+ selector, timeout=timeout, must_be_visible=True
6097
+ )
6098
+ element_text = element.text
6099
+ if self.browser == "safari":
6100
+ element_text = element.get_attribute("innerText")
6101
+ return element_text
6074
6102
6075
6103
def __get_shadow_attribute(self, selector, attribute, timeout):
6076
6104
element = self.__get_shadow_element(selector, timeout=timeout)
@@ -6248,10 +6276,9 @@ def __wait_for_shadow_element_present(self, selector, timeout):
6248
6276
return element
6249
6277
6250
6278
def __wait_for_shadow_element_visible(self, selector, timeout):
6251
- element = self.__get_shadow_element(selector, timeout=timeout)
6252
- if not element.is_displayed():
6253
- msg = "Shadow DOM Element {%s} was not visible!" % selector
6254
- page_actions.timeout_exception("NoSuchElementException", msg)
6279
+ element = self.__get_shadow_element(
6280
+ selector, timeout=timeout, must_be_visible=True
6281
+ )
6255
6282
return element
6256
6283
6257
6284
def __wait_for_shadow_attribute_present(
0 commit comments