|
1 | 1 | """Add CDP methods to extend the driver""" |
2 | 2 | import asyncio |
3 | 3 | import fasteners |
| 4 | +import mycdp |
4 | 5 | import os |
5 | 6 | import random |
6 | 7 | import re |
|
15 | 16 | from seleniumbase.fixtures import page_utils |
16 | 17 | from seleniumbase.fixtures import shared_utils |
17 | 18 | from seleniumbase.undetected.cdp_driver import cdp_util |
| 19 | +from seleniumbase.undetected.cdp_driver import tab as cdp_tab |
18 | 20 |
|
19 | 21 |
|
20 | 22 | class CDPMethods(): |
@@ -1122,9 +1124,33 @@ def switch_to_newest_window(self): |
1122 | 1124 | self.switch_to_tab(-1) |
1123 | 1125 |
|
1124 | 1126 | def open_new_tab(self, url=None, switch_to=True): |
| 1127 | + driver = self.driver |
1125 | 1128 | if not isinstance(url, str): |
1126 | 1129 | url = "about:blank" |
1127 | | - self.loop.run_until_complete(self.page.get(url, new_tab=True)) |
| 1130 | + if hasattr(driver, "cdp_base"): |
| 1131 | + self.loop.run_until_complete(self.page.get(url, new_tab=True)) |
| 1132 | + if switch_to: |
| 1133 | + self.switch_to_newest_tab() |
| 1134 | + return |
| 1135 | + |
| 1136 | + target_id = self.loop.run_until_complete( |
| 1137 | + self.page.send(mycdp.target.create_target(url)) |
| 1138 | + ) |
| 1139 | + found_target = None |
| 1140 | + targets = self.loop.run_until_complete( |
| 1141 | + self.page.send(mycdp.target.get_targets()) |
| 1142 | + ) |
| 1143 | + for target in targets: |
| 1144 | + if str(target_id) in str(target): |
| 1145 | + found_target = target |
| 1146 | + break |
| 1147 | + if found_target: |
| 1148 | + tab_url = driver.tabs[0].websocket_url |
| 1149 | + pre_tab_url = tab_url.split("/page/")[0] + "/page/" |
| 1150 | + new_tab_url = pre_tab_url + target_id |
| 1151 | + new_tab = cdp_tab.Tab(new_tab_url, found_target, driver) |
| 1152 | + driver.targets.append(new_tab) |
| 1153 | + driver.tabs.append(new_tab) |
1128 | 1154 | if switch_to: |
1129 | 1155 | self.switch_to_newest_tab() |
1130 | 1156 |
|
|
0 commit comments