Skip to content

Commit 48c26fa

Browse files
committed
If highlighting elements, use plain javascript in place of jQuery when possible
1 parent 971c899 commit 48c26fa

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,12 @@ def highlight(self, selector, by=By.CSS_SELECTOR,
779779
try:
780780
selector = self.convert_to_css_selector(selector, by=by)
781781
except Exception:
782-
# Don't highlight if can't convert to CSS_SELECTOR for jQuery
782+
# Don't highlight if can't convert to CSS_SELECTOR
783783
return
784-
selector = self._make_css_match_first_element_only(selector)
784+
785+
if self.highlights:
786+
loops = self.highlights
787+
loops = int(loops)
785788

786789
o_bs = '' # original_box_shadow
787790
style = element.get_attribute('style')
@@ -792,13 +795,66 @@ def highlight(self, selector, by=By.CSS_SELECTOR,
792795
original_box_shadow = style[box_start:box_end]
793796
o_bs = original_box_shadow
794797

798+
if ":contains" not in selector and ":first" not in selector:
799+
selector = self.jq_format(selector)
800+
self.__highlight_with_js(selector, loops, scroll, o_bs)
801+
else:
802+
selector = self._make_css_match_first_element_only(selector)
803+
selector = self.jq_format(selector)
804+
try:
805+
self.__highlight_with_jquery(selector, loops, scroll, o_bs)
806+
except Exception:
807+
pass # JQuery probably couldn't load. Skip highlighting.
808+
time.sleep(0.065)
809+
810+
def __highlight_with_js(self, selector, loops, scroll, o_bs):
811+
script = ("""document.querySelector('%s').style =
812+
'box-shadow: 0px 0px 6px 6px rgba(128, 128, 128, 0.5)';"""
813+
% selector)
814+
self.execute_script(script)
815+
816+
for n in range(loops):
817+
script = ("""document.querySelector('%s').style =
818+
'box-shadow: 0px 0px 6px 6px rgba(255, 0, 0, 1)';"""
819+
% selector)
820+
self.execute_script(script)
821+
time.sleep(0.02)
822+
script = ("""document.querySelector('%s').style =
823+
'box-shadow: 0px 0px 6px 6px rgba(128, 0, 128, 1)';"""
824+
% selector)
825+
self.execute_script(script)
826+
time.sleep(0.02)
827+
script = ("""document.querySelector('%s').style =
828+
'box-shadow: 0px 0px 6px 6px rgba(0, 0, 255, 1)';"""
829+
% selector)
830+
self.execute_script(script)
831+
time.sleep(0.02)
832+
script = ("""document.querySelector('%s').style =
833+
'box-shadow: 0px 0px 6px 6px rgba(0, 255, 0, 1)';"""
834+
% selector)
835+
self.execute_script(script)
836+
time.sleep(0.02)
837+
script = ("""document.querySelector('%s').style =
838+
'box-shadow: 0px 0px 6px 6px rgba(128, 128, 0, 1)';"""
839+
% selector)
840+
self.execute_script(script)
841+
time.sleep(0.02)
842+
script = ("""document.querySelector('%s').style =
843+
'box-shadow: 0px 0px 6px 6px rgba(128, 0, 128, 1)';"""
844+
% selector)
845+
self.execute_script(script)
846+
time.sleep(0.02)
847+
848+
script = ("""document.querySelector('%s').style =
849+
'box-shadow: %s';"""
850+
% (selector, o_bs))
851+
self.execute_script(script)
852+
853+
def __highlight_with_jquery(self, selector, loops, scroll, o_bs):
795854
script = """jQuery('%s').css('box-shadow',
796855
'0px 0px 6px 6px rgba(128, 128, 128, 0.5)');""" % selector
797856
self.safe_execute_script(script)
798857

799-
if self.highlights:
800-
loops = self.highlights
801-
loops = int(loops)
802858
for n in range(loops):
803859
script = """jQuery('%s').css('box-shadow',
804860
'0px 0px 6px 6px rgba(255, 0, 0, 1)');""" % selector
@@ -827,7 +883,6 @@ def highlight(self, selector, by=By.CSS_SELECTOR,
827883

828884
script = """jQuery('%s').css('box-shadow', '%s');""" % (selector, o_bs)
829885
self.execute_script(script)
830-
time.sleep(0.065)
831886

832887
def scroll_to(self, selector, by=By.CSS_SELECTOR,
833888
timeout=settings.SMALL_TIMEOUT):

0 commit comments

Comments
 (0)