@@ -657,6 +657,95 @@ def double_click(self, selector, by="css selector", timeout=None):
657657 elif self.__needs_minimum_wait():
658658 time.sleep(0.02)
659659
660+ def context_click(self, selector, by="css selector", timeout=None):
661+ """(A context click is a right-click that opens a context menu.)"""
662+ from selenium.webdriver.common.action_chains import ActionChains
663+
664+ self.__check_scope()
665+ if not timeout:
666+ timeout = settings.SMALL_TIMEOUT
667+ if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
668+ timeout = self.__get_new_timeout(timeout)
669+ original_selector = selector
670+ original_by = by
671+ selector, by = self.__recalculate_selector(selector, by)
672+ element = page_actions.wait_for_element_visible(
673+ self.driver,
674+ selector,
675+ by,
676+ timeout=timeout,
677+ original_selector=original_selector,
678+ )
679+ self.__demo_mode_highlight_if_active(original_selector, original_by)
680+ if not self.demo_mode and not self.slow_mode:
681+ self.__scroll_to_element(element, selector, by)
682+ self.wait_for_ready_state_complete()
683+ if self.__needs_minimum_wait():
684+ time.sleep(0.02)
685+ # Find the element one more time in case scrolling hid it
686+ element = page_actions.wait_for_element_visible(
687+ self.driver,
688+ selector,
689+ by,
690+ timeout=timeout,
691+ original_selector=original_selector,
692+ )
693+ pre_action_url = self.driver.current_url
694+ try:
695+ if self.browser == "safari":
696+ # Jump to the "except" block where the other script should work
697+ raise Exception("This Exception will be caught.")
698+ actions = ActionChains(self.driver)
699+ actions.context_click(element).perform()
700+ except Exception:
701+ css_selector = self.convert_to_css_selector(selector, by=by)
702+ css_selector = re.escape(css_selector) # Add "\\" to special chars
703+ css_selector = self.__escape_quotes_if_needed(css_selector)
704+ right_click_script = (
705+ """var targetElement1 = document.querySelector('%s');
706+ var clickEvent1 = document.createEvent('MouseEvents');
707+ clickEvent1.initEvent('contextmenu', true, true);
708+ targetElement1.dispatchEvent(clickEvent1);"""
709+ % css_selector
710+ )
711+ if ":contains\\(" not in css_selector:
712+ self.execute_script(right_click_script)
713+ else:
714+ right_click_script = (
715+ """jQuery('%s').contextmenu();""" % css_selector
716+ )
717+ self.safe_execute_script(right_click_script)
718+ if settings.WAIT_FOR_RSC_ON_CLICKS:
719+ self.wait_for_ready_state_complete()
720+ else:
721+ # A smaller subset of self.wait_for_ready_state_complete()
722+ self.wait_for_angularjs(timeout=settings.MINI_TIMEOUT)
723+ if self.driver.current_url != pre_action_url:
724+ self.__ad_block_as_needed()
725+ self.__disable_beforeunload_as_needed()
726+ if self.demo_mode:
727+ if self.driver.current_url != pre_action_url:
728+ if not js_utils.is_jquery_activated(self.driver):
729+ js_utils.add_js_link(self.driver, constants.JQuery.MIN_JS)
730+ self.__demo_mode_pause_if_active()
731+ else:
732+ self.__demo_mode_pause_if_active(tiny=True)
733+ elif self.slow_mode:
734+ self.__slow_mode_pause_if_active()
735+ elif self.__needs_minimum_wait():
736+ time.sleep(0.02)
737+ if self.recorder_mode:
738+ url = self.get_current_url()
739+ if url and len(url) > 0:
740+ if ("http:") in url or ("https:") in url or ("file:") in url:
741+ if self.get_session_storage_item("pause_recorder") == "no":
742+ if by == By.XPATH:
743+ selector = original_selector
744+ time_stamp = self.execute_script("return Date.now();")
745+ origin = self.get_origin()
746+ action = ["r_clk", selector, origin, time_stamp]
747+ self.__extra_actions.append(action)
748+
660749 def click_chain(
661750 self, selectors_list, by="css selector", timeout=None, spacing=0
662751 ):
@@ -7573,6 +7662,15 @@ def click_partial_link(self, partial_link_text, timeout=None):
75737662 timeout = self.__get_new_timeout(timeout)
75747663 self.click_partial_link_text(partial_link_text, timeout=timeout)
75757664
7665+ def right_click(self, selector, by="css selector", timeout=None):
7666+ """Same as self.context_click()"""
7667+ self.__check_scope()
7668+ if not timeout:
7669+ timeout = settings.SMALL_TIMEOUT
7670+ if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
7671+ timeout = self.__get_new_timeout(timeout)
7672+ self.context_click(selector, by=by, timeout=timeout)
7673+
75767674 def hover_on_element(self, selector, by="css selector", timeout=None):
75777675 """Same as self.hover()"""
75787676 self.__check_scope()
0 commit comments