Skip to content

Commit

Permalink
fixed find,select for nested frames/shadow-dom
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrafunkamsterdam committed Jul 12, 2024
1 parent cbb8e9a commit e2e85d5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 43 deletions.
6 changes: 6 additions & 0 deletions _update_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ def get_version(project_file: Path):
subprocess.run("copy docs\\_build\\markdown\\README.md .", shell=True)
change_version()
subprocess.run("black nodriver/core *.py")
modified_files = list(
m[1] for m in re.finditer("modified:\s+(.+)", subprocess.getoutput("git status"))
)
subprocess.run("git add " + " ".join(modified_files), shell=True)

subprocess.run("git status")

commit = input("commit message (use no quotes) :")
if commit:
subprocess.run(f'git commit -m "{commit}"')
Expand Down
47 changes: 8 additions & 39 deletions nodriver/core/_contradict.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,28 @@ class ContraDict(dict):

def __init__(self, *args, **kwargs):
super().__init__()
silent = False
try:
silent = kwargs.pop("silent")
except: # noqa
pass
silent = kwargs.pop("silent", False)
_ = dict(*args, **kwargs)
for key, val in _.copy().items():
del _[key]
key = _camel_to_snake(key)
_[key] = val

# for key, val in dict(*args, **kwargs).items():
# _[key] = val
super().__setattr__("__dict__", self)
for k, v in _.items():
_check_key(k, self, False, silent)
super().__setitem__(k, _wrap(self.__class__, v))

def __setitem__(self, key, value):
key = _camel_to_snake(key)
super().__setitem__(key, _wrap(self.__class__, value))

def __setattr__(self, key, value):
key = _camel_to_snake(key)
super().__setitem__(key, _wrap(self.__class__, value))

def __getattribute__(self, attribute):
attribute = _camel_to_snake(attribute)
if not _check_key(attribute, self, boolean=True, silent=True):
return getattr(super(), attribute)
if attribute in self:
return self[attribute]
if not _check_key(attribute, self, True, silent=True):
return getattr(super(), attribute)

return object.__getattribute__(self, attribute)


Expand Down Expand Up @@ -122,32 +115,8 @@ def _check_key(key: str, mapping: _Mapping, boolean: bool = False, silent=False)
return key
if key.lower() in _warning_names or any(_ in key for _ in ("-", ".")):
if not silent:
__logger__.warning(_warning_names_message.format(key))
_warnings.warn(_warning_names_message.format(key))
e = True
if not boolean:
return key
return not e


__RE_CAMEL_TO_SNAKE__ = re.compile("((?!^)(?<!_)[A-Z][a-z]+|(?<=[a-z0-9])[A-Z])")
__RE_SNAKE_TO_CAMEL__ = re.compile("(.*?)_([a-zA-Z])")


def _camel_to_snake(s):
"""
Converts CamelCase/camelCase to snake_case
:param str s: string to be converted
:return: (str) snake_case version of s
"""
s = s.replace("-", "").replace(".", "").replace(" ", "_")

return __RE_CAMEL_TO_SNAKE__.sub(r"_\1", s).lower()


def _snake_to_camel(s):
"""
Converts snake_case_string to camelCaseString
:param str s: string to be converted
:return: (str) camelCase version of s
"""
return __RE_SNAKE_TO_CAMEL__.sub(lambda m: m.group(1) + m.group(2).upper(), s, 0)
9 changes: 9 additions & 0 deletions nodriver/core/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,3 +1154,12 @@ def to_viewport(self, scale=1):

def __repr__(self):
return f"<Position(x={self.left}, y={self.top}, width={self.width}, height={self.height})>"


async def resolve_node(tab: Tab, node_id: cdp.dom.NodeId):
remote_obj: cdp.runtime.RemoteObject = await tab.send(
cdp.dom.resolve_node(node_id=node_id)
)
node_id: cdp.dom.NodeId = await tab.send(cdp.dom.request_node(remote_obj.object_id))
node: cdp.dom.Node = await tab.send(cdp.dom.describe_node(node_id))
return node
2 changes: 1 addition & 1 deletion nodriver/core/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ async def find_element_by_text(
:rtype:
"""
doc = await self.send(cdp.dom.get_document(-1, True))
search_id, nresult = await self.send(cdp.dom.perform_search(text, True))
text = text.strip()
search_id, nresult = await self.send(cdp.dom.perform_search(text, True))

node_ids = await self.send(cdp.dom.get_search_results(search_id, 0, nresult))
await self.send(cdp.dom.discard_search_results(search_id))
Expand Down
9 changes: 7 additions & 2 deletions nodriver/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ def filter_recurse_all(
if predicate(child):
# if predicate is True
out.append(child)
if child.shadow_roots is not None:
out.extend(filter_recurse_all(child.shadow_roots[0], predicate))
out.extend(filter_recurse_all(child, predicate))
# if result:
# out.append(result)

return out


Expand All @@ -182,6 +183,10 @@ def filter_recurse(doc: T, predicate: Callable[[cdp.dom.Node, Element], bool]) -
if predicate(child):
# if predicate is True
return child
if child.shadow_roots:
shadow_root_result = filter_recurse(child.shadow_roots[0], predicate)
if shadow_root_result:
return shadow_root_result
result = filter_recurse(child, predicate)
if result:
return result
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]

name = "nodriver" # Required
version = "0.32" # Required
version = "0.33" # Required

description = """
Expand Down Expand Up @@ -64,6 +64,8 @@ classifiers = [ # Optional
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",

]

Expand Down

0 comments on commit e2e85d5

Please sign in to comment.