(A context click is a right-click that opens a context menu.)
(self, selector, by="css selector", timeout=None)
| 778 | time.sleep(0.02) |
| 779 | |
| 780 | def context_click(self, selector, by="css selector", timeout=None): |
| 781 | """(A context click is a right-click that opens a context menu.)""" |
| 782 | self.__check_scope() |
| 783 | if not timeout: |
| 784 | timeout = settings.SMALL_TIMEOUT |
| 785 | if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT: |
| 786 | timeout = self.__get_new_timeout(timeout) |
| 787 | original_selector = selector |
| 788 | original_by = by |
| 789 | selector, by = self.__recalculate_selector(selector, by) |
| 790 | element = page_actions.wait_for_element_visible( |
| 791 | self.driver, |
| 792 | selector, |
| 793 | by, |
| 794 | timeout=timeout, |
| 795 | original_selector=original_selector, |
| 796 | ) |
| 797 | self.__demo_mode_highlight_if_active(original_selector, original_by) |
| 798 | if not self.demo_mode and not self.slow_mode: |
| 799 | self.__scroll_to_element(element, selector, by) |
| 800 | self.wait_for_ready_state_complete() |
| 801 | if self.__needs_minimum_wait(): |
| 802 | time.sleep(0.02) |
| 803 | # Find the element one more time in case scrolling hid it |
| 804 | element = page_actions.wait_for_element_visible( |
| 805 | self.driver, |
| 806 | selector, |
| 807 | by, |
| 808 | timeout=timeout, |
| 809 | original_selector=original_selector, |
| 810 | ) |
| 811 | pre_action_url = None |
| 812 | with suppress(Exception): |
| 813 | pre_action_url = self.driver.current_url |
| 814 | try: |
| 815 | if self.browser == "safari": |
| 816 | # Jump to the "except" block where the other script should work |
| 817 | raise Exception("This Exception will be caught.") |
| 818 | actions = ActionChains(self.driver) |
| 819 | actions.context_click(element).perform() |
| 820 | except Exception: |
| 821 | css_selector = self.convert_to_css_selector(selector, by=by) |
| 822 | css_selector = re.escape(css_selector) # Add "\\" to special chars |
| 823 | css_selector = self.__escape_quotes_if_needed(css_selector) |
| 824 | right_click_script = ( |
| 825 | """var targetElement1 = document.querySelector('%s'); |
| 826 | var clickEvent1 = document.createEvent('MouseEvents'); |
| 827 | clickEvent1.initEvent('contextmenu', true, true); |
| 828 | targetElement1.dispatchEvent(clickEvent1);""" |
| 829 | % css_selector |
| 830 | ) |
| 831 | if ":contains\\(" not in css_selector: |
| 832 | self.execute_script(right_click_script) |
| 833 | else: |
| 834 | right_click_script = ( |
| 835 | """var targetElement1 = arguments[0]; |
| 836 | var clickEvent1 = document.createEvent('MouseEvents'); |
| 837 | clickEvent1.initEvent('contextmenu', true, true); |