fix selector bug and determine the path according to arch

This commit is contained in:
Jason Lee
2024-02-25 23:15:47 +08:00
parent 3244098664
commit ca24d2a649
21 changed files with 18 additions and 94 deletions

View File

@@ -1,6 +1,7 @@
import json
import logging
import os
import platform
import time
import sqlite3
from typing import Dict, Any, List
@@ -363,10 +364,11 @@ def get_active_url_from_accessTree(env, config):
This function is used to get the active tab url from the accessibility tree.
config:
Dict[str, str]{
'xpath':
the same as in metrics.general.accessibility_tree.
'selectors':
the same as in metrics.general.accessibility_tree.
# we no longer need to specify the xpath or selectors, since we will use defalut value
# 'xpath':
# the same as in metrics.general.accessibility_tree.
# 'selectors':
# the same as in metrics.general.accessibility_tree.
'goto_prefix':
the prefix you want to add to the beginning of the url to be opened, default is "https://",
(the url we get from accTree does not have prefix)
@@ -380,14 +382,17 @@ def get_active_url_from_accessTree(env, config):
logger.debug("AT@eval: %s", accessibility_tree)
# first, use accessibility API to get the active tab URL
at: _Element = lxml.etree.fromstring(accessibility_tree)
if "xpath" in config:
elements: List[_Element] = at.xpath(config["xpath"], namespaces=_accessibility_ns_map)
elif "selectors" in config:
selector = CSSSelector(", ".join(config["selectors"]), namespaces=_accessibility_ns_map)
elements: List[_Element] = selector(at)
arch = platform.machine()
if "arm" in arch:
selector = CSSSelector(", application[name=Chromium] entry[name=Address\\ and\\ search\\ bar]", namespaces=_accessibility_ns_map)
else:
raise ValueError("At least one of xpath and selectors is required")
selector = CSSSelector(", application[name=Google\\ Chrome] entry[name=Address\\ and\\ search\\ bar]", namespaces=_accessibility_ns_map)
elements: List[_Element] = selector(at)
# if "xpath" in config:
# elements: List[_Element] = at.xpath(config["xpath"], namespaces=_accessibility_ns_map)
# elif "selectors" in config:
# selector = CSSSelector(", ".join(config["selectors"]), namespaces=_accessibility_ns_map)
# elements: List[_Element] = selector(at)
if len(elements) == 0:
print("no elements found")
return 0.