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

Method parse_html

webarena/browser_env/processors.py:441–481  ·  view source on GitHub ↗

Parse the html tree into a string text

(dom_tree: DOMTree)

Source from the content-addressed store, hash-verified

439
440 @staticmethod
441 def parse_html(dom_tree: DOMTree) -> tuple[str, dict[str, Any]]:
442 """Parse the html tree into a string text"""
443
444 obs_nodes_info = {}
445 nodeid_to_cursor = {
446 node["nodeId"]: idx for idx, node in enumerate(dom_tree)
447 }
448
449 def dfs(node_cursor: int, depth: int) -> str:
450 tree_str = ""
451 node = dom_tree[node_cursor]
452 indent = "\t" * depth
453 valid_node = True
454 try:
455 node_str = f"[{node_cursor}] <{node['nodeName']}"
456 if node["attributes"]:
457 node_str += f" {node['attributes']}"
458 node_str += f"> {node['nodeValue']}"
459 valid_node = bool(node["attributes"] or node["nodeValue"])
460
461 if valid_node:
462 obs_nodes_info[str(node_cursor)] = {
463 "backend_id": node["backendNodeId"],
464 "union_bound": node["union_bound"],
465 "text": node_str,
466 }
467 tree_str += f"{indent}{node_str}\n"
468
469 except Exception as e:
470 valid_node = False
471
472 for child_ids in node["childIds"]:
473 child_cursor = nodeid_to_cursor[child_ids]
474 child_depth = depth + 1 if valid_node else depth
475 child_str = dfs(child_cursor, child_depth)
476 tree_str += child_str
477
478 return tree_str
479
480 html = dfs(0, 0)
481 return html, obs_nodes_info
482
483 def fetch_page_accessibility_tree(
484 self,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected