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

Method parse

webarena/browser_env/html_tools/html_parser.py:163–280  ·  view source on GitHub ↗
(self, root: html.HtmlElement, keep: list[str], obs: list[str], parent_chain: bool=False, get_new_label: bool=False)

Source from the content-addressed store, hash-verified

161 self.bids2xpath = i2xpath
162
163 def parse(self, root: html.HtmlElement, keep: list[str], obs: list[str], parent_chain: bool=False, get_new_label: bool=False) -> dict[str]:
164 def get_text(str: str) -> str:
165 return '' if str is None else str.strip()[:500]
166
167 def check_attr(attr: str, node: html.HtmlElement) -> bool:
168 tag = node.tag
169 if (
170 ( attr == 'role' and node.attrib.get(attr, '') in ['presentation', 'none', 'link'] )
171 or ( attr == 'type' and node.attrib.get(attr, '') == 'hidden' )
172 # or ( attr == 'value' and tag in ['option'] )
173 ):
174 return False
175 return True
176
177 def is_visible(node: html.HtmlElement, bid: str) -> bool:
178 if self.dataset == 'mind2web':
179 bound = node.attrib.get('bounding_box_rect', None)
180 self.rect[bid] = rect2tuple(bound)
181
182 if not self.use_position:
183 return True
184
185 rect = self.rect.get(bid, None)
186 if rect is None:
187 return False
188
189 if self.window_size is None:
190 return True
191
192 # get window size
193 wx, wy, ww, wh = self.window_size
194 x, y, w, h = rect
195 if x + w < wx or x > wx + ww or y + h < wy or y > wy + wh:
196 return False
197
198 return True
199
200 def _dfs(node: html.HtmlElement, keep: list[str]=[], obs: list[str]=[],
201 parent_chain: bool=False, get_new_label: bool=False, par_keep: bool=False) -> (str, dict[str]):
202 # basic information
203 bid = node.attrib.get(self.id_attr, '')
204 tag = node.tag
205 label = node.attrib.get(self.label_attr, '')
206
207 # element which is keeped equivalent to visible
208 visible = is_visible(node, bid)
209 in_keep_list = bid in keep
210 in_obs_list = (bid in obs or len(label) > 0) and visible
211 keep_element = in_keep_list or in_obs_list or visible or par_keep
212
213 # mark label
214 bids2label, labeled_elems = {}, []
215 have_label = False
216 if in_keep_list or in_obs_list:
217 if label is None or len(label) == 0 or get_new_label:
218 label = self.identifier.generate()
219 node.attrib[self.label_attr] = label
220 bids2label[bid] = label

Callers 3

parse_treeMethod · 0.95
get_segmentMethod · 0.95
parse_playwright_codeFunction · 0.45

Calls 1

_dfsFunction · 0.85

Tested by

no test coverage detected