I am currently writing a python program which uses a seleniumbase web bot with CDP mode activated:
with SB(uc=True, test=True, xvfb=True, incognito=True, agent=<user_agent>, headless=True) as sb:
temp_email_gen_url = "https://temp-mail.org/en"
sb.activate_cdp_mode(temp_email_gen_url)
...
I need to be able to create new tab and switch between the new and original tab. I have read the CDP docs but have not seen a solution to this, does anybody know how this can be done?
For better or worse there isn't an "open tab" feature in CDP mode.
The main developer of seleniumbase
suggests using a separate driver in CDP mode for each tab as follows, equivalent to using "open in new window" on every link:
from seleniumbase import SB
# opens all links on the target page with a second driver
with SB(uc=True, test=True) as sb:
temp_email_gen_url = "https://temp-mail.org/en"
sb.driver.uc_open_with_reconnect(temp_email_gen_url)
links = sb.get_unique_links()
for link in links:
driver2 = sb.get_new_driver(undetectable=True)
driver2.uc_open_with_reconnect(link)
print(driver2.title)
sb.quit_extra_driver()
You may want to consider reusing the second driver for each link instead of creating and destroying a driver for each link. It would be faster and more efficient, but it's possible that the site could use cookies and session storage to detect a suspicious number of page accesses coming from the same browser session.
To elaborate on a question in the comments: there is indeed a way in non-CDP mode to open tabs but I don't recommend it. Connecting the WebDriver leaves traces that bot detection scripts can find, both obvious (in years past hard-coded variable names were added to the JS environment) and subtle such as exploiting obscure behavior around how stack traces and logging commands are buffered and normally run lazily, but not if WebDriver is connected. seleniumbase's UC mode was an attempt at addressing this by using a WebDriver most of the time, but disconnecting for a while just before doing something that can result in detection then waiting until the danger is assumed to have passed before reconnecting. It worked for a while but hosting platforms have adapted. CDP mode is a relatively new entrant in this cat-and-mouse game that is much harder to detect. The growing counter to CDP mode is to track requests and UI interactions such as mouse movements and clicks over time and deploy models like recaptcha v3 that predict the probability a browser is a bot. The counter to that will be increased reliance on pyautogui and similar tools to simulate human interaction with the UI.