@@ -108,6 +108,7 @@ def shadow_root_elements(shadow_root_ds: list[list[str]], element_ds: list[list[
108
108
current_root = generic_driver # Start with main document
109
109
110
110
shadow_root_params = []
111
+ parent_params = []
111
112
for shadow_param in shadow_root_ds :
112
113
left = shadow_param [0 ].strip ().lower ()
113
114
mid = shadow_param [1 ].strip ().lower ()
@@ -119,43 +120,64 @@ def shadow_root_elements(shadow_root_ds: list[list[str]], element_ds: list[list[
119
120
f"Shadow DOM does not support XPath expressions with 'text()'. Please use an attribute-based or tag-based selector instead to identify the element." ,
120
121
3
121
122
)
123
+ return "zeuz_failed"
122
124
123
125
words = mid .strip ().split ()
124
126
if len (words ) < 3 or len (words ) > 4 :
125
127
CommonUtil .ExecLog (sModuleInfo , f"Invalid shadow root parameter format: { mid } " , 3 )
126
128
return "zeuz_failed"
127
129
idx = int (words [1 ]) if len (words ) == 4 else 1
128
130
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"
130
143
131
144
# 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 ))):
134
148
CommonUtil .ExecLog (sModuleInfo , "Duplicate shadow root indices found. Use 'sr 1', 'sr 2', etc." , 3 )
135
149
return "zeuz_failed"
136
150
151
+ parent_params .sort (key = lambda x : x [0 ])
137
152
shadow_root_params .sort (key = lambda x : x [0 ])
138
153
139
154
# Traverse each shadow root level
140
155
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
+
141
162
# 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 )
144
168
145
- # Find shadow host element
146
- elements = current_root .find_elements (By .CSS_SELECTOR , element_query )
147
169
filtered_elements = filter_elements (elements , Filter )
148
170
if not filtered_elements :
149
171
CommonUtil .ExecLog (sModuleInfo , "Shadow host element not found" , 3 )
150
172
return "zeuz_failed"
151
- shadow_host = filtered_elements [index ]
173
+ shadow_host = filtered_elements [shadow_host_index ]
152
174
153
175
# Access the shadow root
154
176
shadow_root = generic_driver .execute_script ('return arguments[0].shadowRoot' , shadow_host )
155
177
if not shadow_root :
156
178
CommonUtil .ExecLog (sModuleInfo , "No shadow root found for element" , 3 )
157
179
return "zeuz_failed"
158
- current_root = shadow_root # Move into the shadow root
180
+ current_root = shadow_root
159
181
160
182
# Locate the target element in the deepest shadow root
161
183
element_query = build_css_selector_query (element_ds )
0 commit comments