MCPcopy Create free account
hub / github.com/THUDM/AutoWebGLM / _dfs

Method _dfs

miniwob++/html_tools/html_parser.py:191–262  ·  view source on GitHub ↗
(node: html.HtmlElement, keep: list[str]=[], obs: list[str]=[], 
                 parent_chain: bool=False, get_new_label: bool=False, par_keep: bool=False)

Source from the content-addressed store, hash-verified

189 return True
190
191 def _dfs(node: html.HtmlElement, keep: list[str]=[], obs: list[str]=[],
192 parent_chain: bool=False, get_new_label: bool=False, par_keep: bool=False) -> (str, dict[str]):
193 # basic information
194 bid = node.attrib.get(self.id_attr, '')
195 tag = node.tag
196 label = node.attrib.get(self.label_attr, '')
197
198 # element which is keeped equivalent to visible
199 visible = is_visible(bid)
200 in_keep_list = bid in keep
201 in_obs_list = (bid in obs or len(label) > 0) and visible
202 keep_element = in_keep_list or in_obs_list or visible or par_keep
203
204 # mark label
205 bids2label, labeled_elems = {}, []
206 have_label = False
207 if in_keep_list or in_obs_list:
208 if label is None or len(label) == 0 or get_new_label:
209 label = self.identifier.generate()
210 node.attrib[self.label_attr] = label
211 bids2label[bid] = label
212 bids2label[label] = bid
213 have_label = True
214
215 # get text or alt_text of current element
216 text = get_text(node.text)
217
218 classes = {}
219 # keep attributes if needed
220 keep_all_attrs = len(self.keep_attrs) == 0
221 keep_attrs = node.attrib.keys() if keep_all_attrs else self.keep_attrs
222
223 # traverse attributes
224 for attr in keep_attrs:
225 if attr not in node.attrib or not check_attr(attr, node):
226 continue
227 if attr in [self.id_attr, self.label_attr]:
228 continue
229 val = get_text(node.attrib[attr])
230 if len(val) > 0 or keep_all_attrs:
231 classes[attr] = val
232
233 have_text = len(text) > 0 or len(classes) > 0
234
235 parts = []
236 clickable_count = 0
237 children = node.getchildren()
238 for child in children:
239 cres, cmsg = _dfs(child, keep, obs, parent_chain, get_new_label)
240 clickable_count += 1 if cmsg.get('have_clickable', False) else 0
241 bids2label.update(cmsg.get('bids2label', {}))
242 labeled_elems.extend(cmsg.get('label_element', []))
243 if len(cres) != 0:
244 parts.append(cres)
245
246 dom = self.prompt.subtree_constructor(parts)
247
248 keep_element = keep_element and (clickable_count > 1 or have_text or have_label)

Callers

nothing calls this directly

Calls 4

_dfsFunction · 0.85
generateMethod · 0.45
subtree_constructorMethod · 0.45
prompt_constructorMethod · 0.45

Tested by

no test coverage detected