Skip to content

Commit

Permalink
fix: don't alter interactive elements CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
adeprez committed Oct 8, 2024
1 parent 4a310a4 commit a3190b6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,22 +325,16 @@ def get_nodes(self, xpaths: List[str]) -> List[SeleniumNode]:

def highlight_nodes(
self, xpaths: List[str], color: str = "red", label=False
) -> Callable:
) -> None:
nodes = self.get_nodes(xpaths)
self.driver.execute_script(ATTACH_MOVE_LISTENER)
set_style = get_highlighter_style(color, label)
self.exec_script_for_nodes(
nodes, "arguments[0].forEach((a, i) => { " + set_style + "})"
)
return self._add_highlighted_destructors(
lambda: self.remove_nodes_highlight(xpaths)
)

def remove_nodes_highlight(self, xpaths: List[str]):
self.exec_script_for_nodes(
self.get_nodes(xpaths),
REMOVE_HIGHLIGHT,
)
def remove_highlight(self):
self.driver.execute_script(REMOVE_HIGHLIGHT)

def exec_script_for_nodes(self, nodes: List[SeleniumNode], script: str):
standard_nodes: List[SeleniumNode] = []
Expand All @@ -365,12 +359,11 @@ def exec_script_for_nodes(self, nodes: List[SeleniumNode], script: str):
if len(special_nodes) > 0:
# iframe and shadow DOM must use the resolve_xpath method
for n in special_nodes:
if n.element:
with n as node:
self.driver.execute_script(
script,
[n.element],
)
self.driver.switch_to.default_content()

def switch_frame(self, xpath: str) -> None:
iframe = self.driver.find_element(By.XPATH, xpath)
Expand Down
63 changes: 24 additions & 39 deletions lavague-sdk/lavague/sdk/base_driver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ def get_nodes(self, xpaths: List[str]) -> List[T]:
@abstractmethod
def highlight_nodes(
self, xpaths: List[str], color: str = "red", label=False
) -> Callable:
) -> None:
pass

@abstractmethod
def remove_highlight(self):
pass

@abstractmethod
Expand All @@ -202,9 +206,9 @@ def switch_parent_frame(self) -> None:
@contextmanager
def nodes_highlighter(self, nodes: List[str], color: str = "red", label=False):
"""Highlight nodes for a context manager"""
remove_highlight = self.highlight_nodes(nodes, color, label)
self.highlight_nodes(nodes, color, label)
yield
remove_highlight()
self.remove_highlight()

def get_obs(self) -> DriverObservation:
"""Get the current observation of the driver"""
Expand Down Expand Up @@ -241,33 +245,13 @@ def get_nodes_from_html(self, html: str) -> List[T]:

def highlight_node_from_xpath(
self, xpath: str, color: str = "red", label=False
) -> Callable:
return self.highlight_nodes([xpath], color, label)
) -> None:
self.highlight_nodes([xpath], color, label)

def highlight_nodes_from_html(
self, html: str, color: str = "blue", label=False
) -> Callable:
return self.highlight_nodes(
re.findall(r"xpath=[\"'](.*?)[\"']", html), color, label
)

def remove_highlight(self):
if hasattr(self, "_highlight_destructors"):
for destructor in self._highlight_destructors:
destructor()
delattr(self, "_highlight_destructors")

def _add_highlighted_destructors(
self, destructors: Union[List[Callable], Callable]
) -> Callable:
if not hasattr(self, "_highlight_destructors"):
self._highlight_destructors = []
if isinstance(destructors, Callable):
self._highlight_destructors.append(destructors)
return destructors

self._highlight_destructors.extend(destructors)
return lambda: [d() for d in destructors]
) -> None:
self.highlight_nodes(re.findall(r"xpath=[\"'](.*?)[\"']", html), color, label)

def highlight_interactive_nodes(
self,
Expand All @@ -278,7 +262,7 @@ def highlight_interactive_nodes(
label=False,
):
if with_interactions is None or len(with_interactions) == 0:
return self.highlight_nodes(
self.highlight_nodes(
list(
self.get_possible_interactions(
in_viewport=in_viewport, foreground_only=foreground_only
Expand All @@ -288,17 +272,18 @@ def highlight_interactive_nodes(
label,
)

return self.highlight_nodes(
[
xpath
for xpath, interactions in self.get_possible_interactions(
in_viewport=in_viewport, foreground_only=foreground_only
).items()
if set(interactions) & set(with_interactions)
],
color,
label,
)
else:
self.highlight_nodes(
[
xpath
for xpath, interactions in self.get_possible_interactions(
in_viewport=in_viewport, foreground_only=foreground_only
).items()
if set(interactions) & set(with_interactions)
],
color,
label,
)

def __enter__(self):
self.init()
Expand Down
4 changes: 1 addition & 3 deletions lavague-sdk/lavague/sdk/base_driver/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ def js_wrap_function_call(fn: str):
ATTACH_MOVE_LISTENER = """
if (!window._lavague_move_listener) {
window._lavague_move_listener = function() {
const bbs = document.querySelectorAll('.lavague-highlight');
bbs.forEach(bb => {
document.querySelectorAll('.lavague-highlight').forEach(bb => {
const rect = bb._tracking.getBoundingClientRect();
bb.style.top = rect.top + 'px';
bb.style.left = rect.left + 'px';
Expand All @@ -277,7 +276,6 @@ def js_wrap_function_call(fn: str):
window.removeEventListener('resize', window._lavague_move_listener);
delete window._lavague_move_listener;
}
arguments[0].filter(a => a).forEach(a => a.style.cssText = a.dataset.originalStyle || '');
document.querySelectorAll('.lavague-highlight').forEach(a => a.remove());
"""

Expand Down

0 comments on commit a3190b6

Please sign in to comment.