Skip to content

Commit 419cedc

Browse files
authored
Merge pull request #2748 from seleniumbase/updates-for-highlighting
Add / improve methods that highlight elements
2 parents 06a495e + d48bb2c commit 419cedc

File tree

6 files changed

+86
-3
lines changed

6 files changed

+86
-3
lines changed

examples/raw_performance_logs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from rich.pretty import pprint
2+
from seleniumbase import SB
3+
4+
with SB(log_cdp=True) as sb:
5+
sb.open("seleniumbase.io/demo_page")
6+
sb.sleep(1)
7+
pprint(sb.driver.get_log("performance"))

examples/test_highlight_elements.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from seleniumbase import BaseCase
2+
BaseCase.main(__name__, __file__)
3+
4+
5+
class HighlightTest(BaseCase):
6+
def test_highlight_inputs(self):
7+
self.open("https://seleniumbase.io/demo_page")
8+
if self.headed:
9+
self.highlight_elements("input", loops=2) # Default: 4
10+
else:
11+
self.highlight_elements("input", loops=1, limit=3)

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ self.highlight_if_visible(selector, by="css selector", loops=4, scroll=True)
386386

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

389+
self.highlight_elements(selector, by="css selector", loops=4, scroll=True, limit=0)
390+
389391
self.press_up_arrow(selector="html", times=1, by="css selector")
390392

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

mkdocs_build/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ lxml==5.2.1
2020
pyquery==2.0.0
2121
readtime==3.0.0
2222
mkdocs==1.6.0
23-
mkdocs-material==9.5.20
23+
mkdocs-material==9.5.21
2424
mkdocs-exclude-search==0.6.6
2525
mkdocs-simple-hooks==0.1.5
2626
mkdocs-material-extensions==1.3.1

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "4.26.2"
2+
__version__ = "4.26.3"

seleniumbase/fixtures/base_case.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def test_anything(self):
6060
from selenium.webdriver.common.by import By
6161
from selenium.webdriver.common.keys import Keys
6262
from selenium.webdriver.remote.remote_connection import LOGGER
63+
from selenium.webdriver.remote.webelement import WebElement
6364
from seleniumbase import config as sb_config
6465
from seleniumbase.__version__ import __version__
6566
from seleniumbase.common import decorators
@@ -5645,6 +5646,40 @@ def highlight_if_visible(
56455646
if self.is_element_visible(selector, by=by):
56465647
self.__highlight(selector, by=by, loops=loops, scroll=scroll)
56475648

5649+
def __highlight_element(self, element, loops=None, scroll=True):
5650+
self.__check_scope()
5651+
if not loops:
5652+
loops = settings.HIGHLIGHTS
5653+
if scroll and self.browser != "safari":
5654+
try:
5655+
self.__slow_scroll_to_element(element)
5656+
except Exception:
5657+
pass
5658+
if self.highlights:
5659+
loops = self.highlights
5660+
if self.browser == "ie":
5661+
loops = 1 # Override previous setting because IE is slow
5662+
loops = int(loops)
5663+
if self.headless or self.headless2 or self.xvfb:
5664+
# Headless modes have less need for highlighting elements.
5665+
# However, highlight() may be used as a sleep alternative.
5666+
loops = int(math.ceil(loops * 0.5))
5667+
o_bs = "" # original_box_shadow
5668+
try:
5669+
style = element.get_attribute("style")
5670+
except Exception:
5671+
self.wait_for_ready_state_complete()
5672+
time.sleep(0.12)
5673+
style = element.get_attribute("style")
5674+
if style:
5675+
if "box-shadow: " in style:
5676+
box_start = style.find("box-shadow: ")
5677+
box_end = style.find(";", box_start) + 1
5678+
original_box_shadow = style[box_start:box_end]
5679+
o_bs = original_box_shadow
5680+
self.__highlight_element_with_js(element, loops, o_bs)
5681+
time.sleep(0.065)
5682+
56485683
def __highlight(
56495684
self, selector, by="css selector", loops=None, scroll=True
56505685
):
@@ -5733,13 +5768,16 @@ def highlight(
57335768
):
57345769
"""This method uses fancy JavaScript to highlight an element.
57355770
@Params
5736-
selector - the selector of the element to find
5771+
selector - the selector of the element to find (Accepts WebElement)
57375772
by - the type of selector to search by (Default: CSS)
57385773
loops - # of times to repeat the highlight animation
57395774
(Default: 4. Each loop lasts for about 0.2s)
57405775
scroll - the option to scroll to the element first (Default: True)
57415776
timeout - the time to wait for the element to appear """
57425777
self.__check_scope()
5778+
if isinstance(selector, WebElement):
5779+
self.__highlight_element(selector, loops=loops, scroll=scroll)
5780+
return
57435781
if not timeout:
57445782
timeout = settings.SMALL_TIMEOUT
57455783
self.wait_for_element_visible(selector, by=by, timeout=timeout)
@@ -5751,6 +5789,31 @@ def highlight(
57515789
action = ["hi_li", selector, origin, time_stamp]
57525790
self.__extra_actions.append(action)
57535791

5792+
def highlight_elements(
5793+
self,
5794+
selector,
5795+
by="css selector",
5796+
loops=None,
5797+
scroll=True,
5798+
limit=0,
5799+
):
5800+
if not limit:
5801+
limit = 0 # 0 means no limit
5802+
limit = int(limit)
5803+
count = 0
5804+
elements = self.find_elements(selector, by=by)
5805+
for element in elements:
5806+
try:
5807+
if element.is_displayed():
5808+
self.__highlight_element(
5809+
element, loops=loops, scroll=scroll
5810+
)
5811+
count += 1
5812+
except Exception:
5813+
pass
5814+
if limit > 0 and count >= limit:
5815+
break
5816+
57545817
def press_up_arrow(self, selector="html", times=1, by="css selector"):
57555818
"""Simulates pressing the UP Arrow on the keyboard.
57565819
By default, "html" will be used as the CSS Selector target.

0 commit comments

Comments
 (0)