Clicks an element using JavaScript. Can be used to click hidden / invisible elements. If "all_matches" is False, only the first match is clicked. If "scroll" is False, won't scroll unless running in Demo Mode.
(
self,
selector,
by="css selector",
all_matches=False,
timeout=None,
scroll=True,
)
| 7025 | self.click(xpath, by="xpath") |
| 7026 | |
| 7027 | def js_click( |
| 7028 | self, |
| 7029 | selector, |
| 7030 | by="css selector", |
| 7031 | all_matches=False, |
| 7032 | timeout=None, |
| 7033 | scroll=True, |
| 7034 | ): |
| 7035 | """Clicks an element using JavaScript. |
| 7036 | Can be used to click hidden / invisible elements. |
| 7037 | If "all_matches" is False, only the first match is clicked. |
| 7038 | If "scroll" is False, won't scroll unless running in Demo Mode.""" |
| 7039 | if self.__is_cdp_swap_needed(): |
| 7040 | self.cdp.click(selector, timeout=timeout) |
| 7041 | return |
| 7042 | self.wait_for_ready_state_complete() |
| 7043 | if not timeout or timeout is True: |
| 7044 | timeout = settings.SMALL_TIMEOUT |
| 7045 | if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT: |
| 7046 | timeout = self.__get_new_timeout(timeout) |
| 7047 | selector, by = self.__recalculate_selector(selector, by, xp_ok=False) |
| 7048 | if by == By.LINK_TEXT: |
| 7049 | message = ( |
| 7050 | "Pure JavaScript doesn't support clicking by Link Text. " |
| 7051 | "You may want to use self.jquery_click() instead, which " |
| 7052 | "allows this with :contains(), assuming jQuery isn't blocked. " |
| 7053 | "For now, self.js_click() will use a regular WebDriver click." |
| 7054 | ) |
| 7055 | logging.debug(message) |
| 7056 | self.click(selector, by=by) |
| 7057 | return |
| 7058 | element = self.wait_for_element_present( |
| 7059 | selector, by=by, timeout=timeout |
| 7060 | ) |
| 7061 | if not page_actions.is_element_clickable(self.driver, selector, by): |
| 7062 | self.wait_for_ready_state_complete() |
| 7063 | scroll_done = False |
| 7064 | if self.is_element_visible(selector, by=by): |
| 7065 | scroll_done = True |
| 7066 | self.__demo_mode_highlight_if_active(selector, by) |
| 7067 | if scroll and not self.demo_mode and not self.slow_mode: |
| 7068 | success = js_utils.scroll_to_element(self.driver, element) |
| 7069 | if not success: |
| 7070 | self.wait_for_ready_state_complete() |
| 7071 | timeout = settings.SMALL_TIMEOUT |
| 7072 | element = page_actions.wait_for_element_present( |
| 7073 | self.driver, selector, by, timeout=timeout |
| 7074 | ) |
| 7075 | css_selector = self.convert_to_css_selector(selector, by=by) |
| 7076 | css_selector = re.escape(css_selector) # Add "\\" to special chars |
| 7077 | css_selector = self.__escape_quotes_if_needed(css_selector) |
| 7078 | time_stamp = 0 |
| 7079 | action = ["", "", "", time_stamp] |
| 7080 | pre_action_url = None |
| 7081 | with suppress(Exception): |
| 7082 | pre_action_url = self.driver.current_url |
| 7083 | pre_window_count = len(self.driver.window_handles) |
| 7084 | if self.recorder_mode and not self.__dont_record_js_click: |