Skip to content

Add / improve methods that highlight elements #2748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/raw_performance_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rich.pretty import pprint
from seleniumbase import SB

with SB(log_cdp=True) as sb:
sb.open("seleniumbase.io/demo_page")
sb.sleep(1)
pprint(sb.driver.get_log("performance"))
11 changes: 11 additions & 0 deletions examples/test_highlight_elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)


class HighlightTest(BaseCase):
def test_highlight_inputs(self):
self.open("https://seleniumbase.io/demo_page")
if self.headed:
self.highlight_elements("input", loops=2) # Default: 4
else:
self.highlight_elements("input", loops=1, limit=3)
2 changes: 2 additions & 0 deletions help_docs/method_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ self.highlight_if_visible(selector, by="css selector", loops=4, scroll=True)

self.highlight(selector, by="css selector", loops=4, scroll=True, timeout=None)

self.highlight_elements(selector, by="css selector", loops=4, scroll=True, limit=0)

self.press_up_arrow(selector="html", times=1, by="css selector")

self.press_down_arrow(selector="html", times=1, by="css selector")
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_build/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lxml==5.2.1
pyquery==2.0.0
readtime==3.0.0
mkdocs==1.6.0
mkdocs-material==9.5.20
mkdocs-material==9.5.21
mkdocs-exclude-search==0.6.6
mkdocs-simple-hooks==0.1.5
mkdocs-material-extensions==1.3.1
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.26.2"
__version__ = "4.26.3"
65 changes: 64 additions & 1 deletion seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def test_anything(self):
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.remote_connection import LOGGER
from selenium.webdriver.remote.webelement import WebElement
from seleniumbase import config as sb_config
from seleniumbase.__version__ import __version__
from seleniumbase.common import decorators
Expand Down Expand Up @@ -5645,6 +5646,40 @@ def highlight_if_visible(
if self.is_element_visible(selector, by=by):
self.__highlight(selector, by=by, loops=loops, scroll=scroll)

def __highlight_element(self, element, loops=None, scroll=True):
self.__check_scope()
if not loops:
loops = settings.HIGHLIGHTS
if scroll and self.browser != "safari":
try:
self.__slow_scroll_to_element(element)
except Exception:
pass
if self.highlights:
loops = self.highlights
if self.browser == "ie":
loops = 1 # Override previous setting because IE is slow
loops = int(loops)
if self.headless or self.headless2 or self.xvfb:
# Headless modes have less need for highlighting elements.
# However, highlight() may be used as a sleep alternative.
loops = int(math.ceil(loops * 0.5))
o_bs = "" # original_box_shadow
try:
style = element.get_attribute("style")
except Exception:
self.wait_for_ready_state_complete()
time.sleep(0.12)
style = element.get_attribute("style")
if style:
if "box-shadow: " in style:
box_start = style.find("box-shadow: ")
box_end = style.find(";", box_start) + 1
original_box_shadow = style[box_start:box_end]
o_bs = original_box_shadow
self.__highlight_element_with_js(element, loops, o_bs)
time.sleep(0.065)

def __highlight(
self, selector, by="css selector", loops=None, scroll=True
):
Expand Down Expand Up @@ -5733,13 +5768,16 @@ def highlight(
):
"""This method uses fancy JavaScript to highlight an element.
@Params
selector - the selector of the element to find
selector - the selector of the element to find (Accepts WebElement)
by - the type of selector to search by (Default: CSS)
loops - # of times to repeat the highlight animation
(Default: 4. Each loop lasts for about 0.2s)
scroll - the option to scroll to the element first (Default: True)
timeout - the time to wait for the element to appear """
self.__check_scope()
if isinstance(selector, WebElement):
self.__highlight_element(selector, loops=loops, scroll=scroll)
return
if not timeout:
timeout = settings.SMALL_TIMEOUT
self.wait_for_element_visible(selector, by=by, timeout=timeout)
Expand All @@ -5751,6 +5789,31 @@ def highlight(
action = ["hi_li", selector, origin, time_stamp]
self.__extra_actions.append(action)

def highlight_elements(
self,
selector,
by="css selector",
loops=None,
scroll=True,
limit=0,
):
if not limit:
limit = 0 # 0 means no limit
limit = int(limit)
count = 0
elements = self.find_elements(selector, by=by)
for element in elements:
try:
if element.is_displayed():
self.__highlight_element(
element, loops=loops, scroll=scroll
)
count += 1
except Exception:
pass
if limit > 0 and count >= limit:
break

def press_up_arrow(self, selector="html", times=1, by="css selector"):
"""Simulates pressing the UP Arrow on the keyboard.
By default, "html" will be used as the CSS Selector target.
Expand Down