1414from unbrowsed .types import AriaRoles
1515from unbrowsed .resolvers import RoleResolver
1616
17+ SELECTOR = "*:not(html):not(body)"
18+
1719
1820class QueryResult :
1921 """Wrapper class for query result."""
@@ -170,29 +172,22 @@ def query_by_text(dom: Parser, text: str, exact=True) -> Optional[QueryResult]:
170172 search_text = TextMatch (text , exact = exact )
171173 matches = []
172174
173- for element in dom .css ("*" ):
175+ for element in dom .css (SELECTOR ):
174176 element_text = element .text (deep = True , strip = True )
175177
176178 if search_text .matches (element_text ):
177179 matches .append (element )
178180
179181 if len (matches ) > 1 :
180- exclusions = ("html" , "body" )
181- filtered_matches = [m for m in matches if m .tag not in exclusions ]
182-
183- if filtered_matches :
184- matches = filtered_matches
185-
186- if len (matches ) > 1 :
187- for i , parent in enumerate (matches ):
188- for j , child in enumerate (matches ):
189- if i != j and is_parent_of (parent , child ):
190- return QueryResult (matches [i ])
182+ for i , parent in enumerate (matches ):
183+ for j , child in enumerate (matches ):
184+ if i != j and is_parent_of (parent , child ):
185+ return QueryResult (matches [i ])
191186
192- raise MultipleElementsFoundError (
193- f"Found { len (matches )} elements with text '{ text } '. "
194- f"Use query_all_by_text if multiple matches are expected."
195- )
187+ raise MultipleElementsFoundError (
188+ f"Found { len (matches )} elements with text '{ text } '. "
189+ f"Use query_all_by_text if multiple matches are expected."
190+ )
196191
197192 if not matches :
198193 return None
@@ -276,7 +271,7 @@ def query_by_role(
276271 )
277272 matches = []
278273
279- for element in dom .css ("*" ):
274+ for element in dom .css (SELECTOR ):
280275 if not role_matcher .matches (element ):
281276 continue
282277
@@ -288,13 +283,6 @@ def query_by_role(
288283
289284 matches .append (element )
290285
291- if len (matches ) > 1 :
292- exclusions = ("html" , "body" )
293- filtered_matches = [m for m in matches if m .tag not in exclusions ]
294-
295- if filtered_matches :
296- matches = filtered_matches
297-
298286 if len (matches ) > 1 :
299287 for i , parent in enumerate (matches ):
300288 for j , child in enumerate (matches ):
0 commit comments