Skip to content

Commit d7d406d

Browse files
committed
detect by parent is now supports
1 parent 523d806 commit d7d406d

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

Framework/Built_In_Automation/Shared_Resources/LocateElement.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def shadow_root_elements(shadow_root_ds: list[list[str]], element_ds: list[list[
108108
current_root = generic_driver # Start with main document
109109

110110
shadow_root_params = []
111+
parent_params = []
111112
for shadow_param in shadow_root_ds:
112113
left = shadow_param[0].strip().lower()
113114
mid = shadow_param[1].strip().lower()
@@ -119,43 +120,64 @@ def shadow_root_elements(shadow_root_ds: list[list[str]], element_ds: list[list[
119120
f"Shadow DOM does not support XPath expressions with 'text()'. Please use an attribute-based or tag-based selector instead to identify the element.",
120121
3
121122
)
123+
return "zeuz_failed"
122124

123125
words = mid.strip().split()
124126
if len(words) < 3 or len(words) > 4:
125127
CommonUtil.ExecLog(sModuleInfo, f"Invalid shadow root parameter format: {mid}", 3)
126128
return "zeuz_failed"
127129
idx = int(words[1]) if len(words) == 4 else 1
128130
param = ' '.join(words[-2:])
129-
shadow_root_params.append((idx, [left, param, right]))
131+
132+
if "parent" in param:
133+
parent_params.append((idx, [left, param, right]))
134+
elif "element" in param:
135+
shadow_root_params.append((idx, [left, param, right]))
136+
else:
137+
CommonUtil.ExecLog(
138+
sModuleInfo,
139+
f"Invalid parameter '{param}' encountered for shadow DOM access. Only 'parent parameter' and 'element parameter' are supported.",
140+
3
141+
)
142+
return "zeuz_failed"
130143

131144
# Check for duplicate indices
132-
indices = [idx for idx, _ in shadow_root_params]
133-
if len(indices) != len(set(indices)):
145+
indices1 = [idx for idx, _ in parent_params]
146+
indices2 = [idx for idx, _ in shadow_root_params]
147+
if (len(indices1) != len(set(indices1))) or (len(indices2) != len(set(indices2))):
134148
CommonUtil.ExecLog(sModuleInfo, "Duplicate shadow root indices found. Use 'sr 1', 'sr 2', etc.", 3)
135149
return "zeuz_failed"
136150

151+
parent_params.sort(key=lambda x: x[0])
137152
shadow_root_params.sort(key=lambda x: x[0])
138153

139154
# Traverse each shadow root level
140155
for idx, shadow_param in shadow_root_params:
156+
shadow_host_query = None
157+
for idx2, parent_param in parent_params:
158+
if idx == idx2:
159+
shadow_host_query = build_css_selector_query([parent_param, shadow_param])
160+
break
161+
141162
# Build CSS selector for the current shadow host
142-
element_query = build_css_selector_query([shadow_param])
143-
index = _locate_index_number([shadow_param]) or 0
163+
if not shadow_host_query:
164+
shadow_host_query = build_css_selector_query([shadow_param])
165+
shadow_host_index = _locate_index_number([shadow_param]) or 0
166+
167+
elements = current_root.find_elements(By.CSS_SELECTOR, shadow_host_query)
144168

145-
# Find shadow host element
146-
elements = current_root.find_elements(By.CSS_SELECTOR, element_query)
147169
filtered_elements = filter_elements(elements, Filter)
148170
if not filtered_elements:
149171
CommonUtil.ExecLog(sModuleInfo, "Shadow host element not found", 3)
150172
return "zeuz_failed"
151-
shadow_host = filtered_elements[index]
173+
shadow_host = filtered_elements[shadow_host_index]
152174

153175
# Access the shadow root
154176
shadow_root = generic_driver.execute_script('return arguments[0].shadowRoot', shadow_host)
155177
if not shadow_root:
156178
CommonUtil.ExecLog(sModuleInfo, "No shadow root found for element", 3)
157179
return "zeuz_failed"
158-
current_root = shadow_root # Move into the shadow root
180+
current_root = shadow_root
159181

160182
# Locate the target element in the deepest shadow root
161183
element_query = build_css_selector_query(element_ds)

0 commit comments

Comments
 (0)