Skip to content

Commit 93a4f4f

Browse files
committed
xpath/css selector for first level element detection in shadow dom
1 parent d7d406d commit 93a4f4f

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

Framework/Built_In_Automation/Shared_Resources/LocateElement.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,37 @@ def shadow_root_elements(shadow_root_ds: list[list[str]], element_ds: list[list[
154154
# Traverse each shadow root level
155155
for idx, shadow_param in shadow_root_params:
156156
shadow_host_query = None
157+
locator_type = None
157158
for idx2, parent_param in parent_params:
158159
if idx == idx2:
159-
shadow_host_query = build_css_selector_query([parent_param, shadow_param])
160+
if idx == 1:
161+
shadow_host_query, query_type = _construct_query([parent_param, shadow_param])
162+
locator_type = By.XPATH if query_type == "xpath" else By.CSS_SELECTOR
163+
else:
164+
shadow_host_query = build_css_selector_query([parent_param, shadow_param])
160165
break
161166

162167
# Build CSS selector for the current shadow host
163168
if not shadow_host_query:
164-
shadow_host_query = build_css_selector_query([shadow_param])
169+
if idx == 1: # First shadow root without parent, use XPath
170+
shadow_host_query, query_type = _construct_query([shadow_param])
171+
locator_type = By.XPATH if query_type == "xpath" else By.CSS_SELECTOR
172+
else:
173+
shadow_host_query = build_css_selector_query([shadow_param])
174+
locator_type = By.CSS_SELECTOR
165175
shadow_host_index = _locate_index_number([shadow_param]) or 0
166176

167-
elements = current_root.find_elements(By.CSS_SELECTOR, shadow_host_query)
177+
CommonUtil.ExecLog(
178+
sModuleInfo,
179+
f"To locate the Element we used {locator_type}:\n{shadow_host_query}",
180+
5
181+
)
182+
183+
elements = None
184+
if locator_type == By.XPATH:
185+
elements = current_root.find_elements(By.XPATH, shadow_host_query)
186+
else:
187+
elements = current_root.find_elements(By.CSS_SELECTOR, shadow_host_query)
168188

169189
filtered_elements = filter_elements(elements, Filter)
170190
if not filtered_elements:
@@ -549,7 +569,7 @@ def _construct_query(step_data_set, web_element_object=False):
549569
and driver_type in ("appium", "selenium")
550570
): # for unique identifier
551571
return [unique_parameter_list[0][0], unique_parameter_list[0][2]], "unique"
552-
elif "css" in collect_all_attribute and "xpath" not in collect_all_attribute:
572+
elif "css_selector" in collect_all_attribute and "xpath" not in collect_all_attribute:
553573
# return the raw css command with css as type. We do this so that even if user enters other data, we will ignore them.
554574
# here we expect to get raw css query
555575
return ([x for x in step_data_set if "css" in x[0]][0][2]), "css"

0 commit comments

Comments
 (0)