Skip to content

Commit 039e52a

Browse files
committed
Add methods for pressing arrow keys
1 parent 0b1662a commit 039e52a

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

help_docs/method_summary.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ self.highlight_update_text(selector, new_value, by=By.CSS_SELECTOR, loops=3, scr
187187

188188
self.highlight(selector, by=By.CSS_SELECTOR, loops=4, scroll=True)
189189

190+
self.press_up_arrow(selector="html", times=1, by=By.CSS_SELECTOR)
191+
192+
self.press_down_arrow(selector="html", times=1, by=By.CSS_SELECTOR)
193+
194+
self.press_left_arrow(selector="html", times=1, by=By.CSS_SELECTOR)
195+
196+
self.press_right_arrow(selector="html", times=1, by=By.CSS_SELECTOR)
197+
190198
self.scroll_to(selector, by=By.CSS_SELECTOR)
191199

192200
self.slow_scroll_to(selector, by=By.CSS_SELECTOR)

seleniumbase/fixtures/base_case.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,90 @@ def __highlight_with_js(self, selector, loops, o_bs):
19481948
def __highlight_with_jquery(self, selector, loops, o_bs):
19491949
js_utils.highlight_with_jquery(self.driver, selector, loops, o_bs)
19501950

1951+
def press_up_arrow(self, selector="html", times=1, by=By.CSS_SELECTOR):
1952+
""" Simulates pressing the UP Arrow on the keyboard.
1953+
By default, "html" will be used as the CSS Selector target.
1954+
You can specify how many times in-a-row the action happens. """
1955+
if times < 1:
1956+
return
1957+
element = self.wait_for_element_present(selector)
1958+
self.__demo_mode_highlight_if_active(selector, by)
1959+
if not self.demo_mode:
1960+
self.__scroll_to_element(element, selector, by)
1961+
for i in range(int(times)):
1962+
try:
1963+
element.send_keys(Keys.ARROW_UP)
1964+
except Exception:
1965+
self.wait_for_ready_state_complete()
1966+
element = self.wait_for_element_visible(selector)
1967+
element.send_keys(Keys.ARROW_UP)
1968+
time.sleep(0.01)
1969+
if self.slow_mode:
1970+
time.sleep(0.1)
1971+
1972+
def press_down_arrow(self, selector="html", times=1, by=By.CSS_SELECTOR):
1973+
""" Simulates pressing the DOWN Arrow on the keyboard.
1974+
By default, "html" will be used as the CSS Selector target.
1975+
You can specify how many times in-a-row the action happens. """
1976+
if times < 1:
1977+
return
1978+
element = self.wait_for_element_present(selector)
1979+
self.__demo_mode_highlight_if_active(selector, by)
1980+
if not self.demo_mode:
1981+
self.__scroll_to_element(element, selector, by)
1982+
for i in range(int(times)):
1983+
try:
1984+
element.send_keys(Keys.ARROW_DOWN)
1985+
except Exception:
1986+
self.wait_for_ready_state_complete()
1987+
element = self.wait_for_element_visible(selector)
1988+
element.send_keys(Keys.ARROW_DOWN)
1989+
time.sleep(0.01)
1990+
if self.slow_mode:
1991+
time.sleep(0.1)
1992+
1993+
def press_left_arrow(self, selector="html", times=1, by=By.CSS_SELECTOR):
1994+
""" Simulates pressing the LEFT Arrow on the keyboard.
1995+
By default, "html" will be used as the CSS Selector target.
1996+
You can specify how many times in-a-row the action happens. """
1997+
if times < 1:
1998+
return
1999+
element = self.wait_for_element_present(selector)
2000+
self.__demo_mode_highlight_if_active(selector, by)
2001+
if not self.demo_mode:
2002+
self.__scroll_to_element(element, selector, by)
2003+
for i in range(int(times)):
2004+
try:
2005+
element.send_keys(Keys.ARROW_LEFT)
2006+
except Exception:
2007+
self.wait_for_ready_state_complete()
2008+
element = self.wait_for_element_visible(selector)
2009+
element.send_keys(Keys.ARROW_LEFT)
2010+
time.sleep(0.01)
2011+
if self.slow_mode:
2012+
time.sleep(0.1)
2013+
2014+
def press_right_arrow(self, selector="html", times=1, by=By.CSS_SELECTOR):
2015+
""" Simulates pressing the RIGHT Arrow on the keyboard.
2016+
By default, "html" will be used as the CSS Selector target.
2017+
You can specify how many times in-a-row the action happens. """
2018+
if times < 1:
2019+
return
2020+
element = self.wait_for_element_present(selector)
2021+
self.__demo_mode_highlight_if_active(selector, by)
2022+
if not self.demo_mode:
2023+
self.__scroll_to_element(element, selector, by)
2024+
for i in range(int(times)):
2025+
try:
2026+
element.send_keys(Keys.ARROW_RIGHT)
2027+
except Exception:
2028+
self.wait_for_ready_state_complete()
2029+
element = self.wait_for_element_visible(selector)
2030+
element.send_keys(Keys.ARROW_RIGHT)
2031+
time.sleep(0.01)
2032+
if self.slow_mode:
2033+
time.sleep(0.1)
2034+
19512035
def scroll_to(self, selector, by=By.CSS_SELECTOR, timeout=None):
19522036
''' Fast scroll to destination '''
19532037
if not timeout:
@@ -1987,6 +2071,7 @@ def slow_scroll_to(self, selector, by=By.CSS_SELECTOR, timeout=None):
19872071
self.__slow_scroll_to_element(element)
19882072

19892073
def scroll_to_top(self):
2074+
""" Scroll to the top of the page. """
19902075
scroll_script = "window.scrollTo(0, 0);"
19912076
try:
19922077
self.execute_script(scroll_script)
@@ -1996,6 +2081,7 @@ def scroll_to_top(self):
19962081
return False
19972082

19982083
def scroll_to_bottom(self):
2084+
""" Scroll to the bottom of the page. """
19992085
scroll_script = "window.scrollTo(0, 10000);"
20002086
try:
20012087
self.execute_script(scroll_script)

0 commit comments

Comments
 (0)