Check whether the contents appear in the page
| 242 | |
| 243 | |
| 244 | class HTMLContentEvaluator(Evaluator): |
| 245 | """Check whether the contents appear in the page""" |
| 246 | |
| 247 | @beartype |
| 248 | def __call__( |
| 249 | self, |
| 250 | trajectory: Trajectory, |
| 251 | config_file: Path | str, |
| 252 | page: Page | PseudoPage, |
| 253 | client: CDPSession | None = None, |
| 254 | ) -> float: |
| 255 | with open(config_file, "r") as f: |
| 256 | configs = json.load(f) |
| 257 | |
| 258 | targets = configs["eval"]["program_html"] |
| 259 | |
| 260 | score = 1.0 |
| 261 | for target in targets: |
| 262 | target_url: str = target["url"] # which url to check |
| 263 | if target_url.startswith("func"): |
| 264 | func = target_url.split("func:")[1] |
| 265 | func = func.replace("__last_url__", page.url) |
| 266 | target_url = eval(func) |
| 267 | |
| 268 | locator: str = target["locator"] # js element locator |
| 269 | |
| 270 | # navigate to that url |
| 271 | if target_url != "last": |
| 272 | page.goto(target_url) |
| 273 | time.sleep(3) # TODO [shuyanzh]: fix this hard-coded sleep |
| 274 | |
| 275 | # empty, use the full page |
| 276 | if not locator.strip(): |
| 277 | selected_element = page.content() |
| 278 | # use JS to select the element |
| 279 | elif locator.startswith("document.") or locator.startswith( |
| 280 | "[...document." |
| 281 | ): |
| 282 | if "prep_actions" in target: |
| 283 | try: |
| 284 | for prep_action in target["prep_actions"]: |
| 285 | page.evaluate(f"() => {prep_action}") |
| 286 | except Exception: |
| 287 | pass |
| 288 | try: |
| 289 | selected_element = str(page.evaluate(f"() => {locator}")) |
| 290 | if not selected_element: |
| 291 | selected_element = "" |
| 292 | except Exception: |
| 293 | # the page is wrong, return empty |
| 294 | selected_element = "" |
| 295 | # run program to call API |
| 296 | elif locator.startswith("func:"): # a helper function |
| 297 | func = locator.split("func:")[1] |
| 298 | func = func.replace("__page__", "page") |
| 299 | selected_element = eval(func) |
| 300 | else: |
| 301 | raise ValueError(f"Unknown locator: {locator}") |
no outgoing calls