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

Class HtmlParser

webarena/browser_env/html_tools/html_parser.py:10–425  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8from .utils import get_xpath_top_down, rect2tuple
9
10class HtmlParser():
11 def __init__(self, ctx: str, args: dict[str]={}) -> None:
12 stt = time.time()
13 self.dom_tree = self.ctx2tree(ctx)
14 # tool related
15 self.bids2label = {}
16 self.bids2xpath = {}
17 self.used_labels = {}
18
19 # parse args
20 self.parse_args(args)
21 self.init_time = time.time() - stt
22
23 def parse_args(self, args: dict[str]={}) -> None:
24 def attr_check(attr, type_model='str'):
25 if attr is None:
26 return False
27 attr_type = type(attr)
28 if attr_type != type(type_model):
29 return False
30 if attr_type == type('str') and len(attr) == 0:
31 return False
32 return True
33
34 args = {} if args is None else args
35
36 # [Position] use_pos: False -> use full page, otherwise use window_size
37 dataset = args.get('dataset', '')
38 use_position = args.get('use_position', False)
39 window_size = args.get('window_size', None)
40 rect = args.get('rect_dict', None)
41 if use_position:
42 if not attr_check(window_size, ()):
43 raise ValueError('window_size must be set when use_position is True')
44 if not attr_check(rect, {}):
45 raise ValueError('rect_dict must be set when use_position is True')
46
47 if not attr_check(rect, {}):
48 rect = {}
49
50 # [Label] for vimium is temp_clickable_label, otherwise keep all of it
51 label_attr = args.get('label_attr', '')
52 get_new_label = args.get('regenerate_label', False)
53 label_method = args.get('label_generator', None)
54 regen_label = not attr_check(label_method)
55
56 # [id] for mind2web is backend_node_id, for normal website use our method
57 id_attr = args.get('id_attr', '')
58 regen_id = not attr_check(id_attr)
59
60 if regen_id:
61 id_attr = 'temp_id'
62
63 # [attributes]
64 keep_attrs = args.get('attr_list', [])
65 if not attr_check(keep_attrs, []):
66 keep_attrs = []
67

Callers 1

parse_my_htmlMethod · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected