Skip to content

Commit 5baddcd

Browse files
committed
If a click opens a new tab, switch to it automatically
1 parent 4092d0c commit 5baddcd

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

seleniumbase/config/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
HTML_REPORT = "report.html"
3939
RESULTS_TABLE = "results_table.csv"
4040

41+
"""
42+
If True, switch to new tabs/windows automatically if a click opens a new one.
43+
(This switch only happens if the initial tab is still on same URL as before,
44+
which prevents a situation where a click opens up a new URL in the same tab,
45+
where a pop-up might open up a new tab on its own, leading to a double open.
46+
If False, the browser will stay on the current tab where the click happened.
47+
"""
48+
SWITCH_TO_NEW_TABS_ON_CLICK = True
49+
4150
"""
4251
This adds wait_for_ready_state_complete() after various browser actions.
4352
Setting this to True may improve reliability at the cost of speed.

seleniumbase/fixtures/base_case.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,18 @@ def click(
316316
self.driver, selector, by, timeout=timeout
317317
)
318318
element.click()
319-
if self.recorder_mode:
320-
latest_window_count = len(self.driver.window_handles)
321-
if latest_window_count > pre_window_count:
322-
self.switch_to_newest_window()
319+
latest_window_count = len(self.driver.window_handles)
320+
if (
321+
latest_window_count > pre_window_count
322+
and (
323+
self.recorder_mode
324+
or (
325+
settings.SWITCH_TO_NEW_TABS_ON_CLICK
326+
and self.driver.current_url == pre_action_url
327+
)
328+
)
329+
):
330+
self.switch_to_newest_window()
323331
if settings.WAIT_FOR_RSC_ON_CLICKS:
324332
self.wait_for_ready_state_complete()
325333
if self.demo_mode:
@@ -3721,6 +3729,20 @@ def js_click(
37213729
else:
37223730
click_script = """jQuery('%s').click();""" % css_selector
37233731
self.safe_execute_script(click_script)
3732+
if self.recorder_mode and action:
3733+
self.__extra_actions.append(action)
3734+
latest_window_count = len(self.driver.window_handles)
3735+
if (
3736+
latest_window_count > pre_window_count
3737+
and (
3738+
self.recorder_mode
3739+
or (
3740+
settings.SWITCH_TO_NEW_TABS_ON_CLICK
3741+
and self.driver.current_url == pre_action_url
3742+
)
3743+
)
3744+
):
3745+
self.switch_to_newest_window()
37243746
self.wait_for_ready_state_complete()
37253747
self.__demo_mode_pause_if_active()
37263748

@@ -9862,6 +9884,10 @@ def setUp(self, masterqa_mode=False):
98629884
# Only filled when Recorder Mode is enabled
98639885
sb_config._recorded_actions = {}
98649886

9887+
if not hasattr(settings, "SWITCH_TO_NEW_TABS_ON_CLICK"):
9888+
# If using an older settings file, set the new definitions manually
9889+
settings.SWITCH_TO_NEW_TABS_ON_CLICK = True
9890+
98659891
# Parse the settings file
98669892
if self.settings_file:
98679893
from seleniumbase.core import settings_parser

0 commit comments

Comments
 (0)