How to set window size in Selenium Base in CDP Mode?
11:00 03 Feb 2025

I am using SeleniumBase in CDP Mode.

I am trying to figure out how to set the window size in CDP Mode AFTER the website is loaded?

If I use the non CDP function sb.set_window_size(x,y), then it gets detected as bot runtimeEnableLeak for opening devtools on https://bot-detector.rebrowser.net:

enter image description here

I tried sb.cdp.set_window_size(x,y) but that function doesn't seem to exist as it crashes with:

sb.cdp.set_window_size(x,y)
AttributeError: 'types.SimpleNamespace' object has no attribute 'set_window_size'

EDIT: I was able to find a workaround using sb.cdp.set_window_rect:

[screenwidth,screenheight,innerwidth,innerheight,scrollwidth,scrollheight] = sb.cdp.evaluate("return [window.screen.width, window.screen.height, window.innerWidth, window.innerHeight, document.documentElement.scrollWidth, document.documentElement.scrollHeight];")

print(f"Size: {screenwidth}, {screenheight}, {innerwidth}, {innerheight}, {scrollwidth}, {scrollheight}")

sb.cdp.set_window_rect(0,0,scrollwidth + screenwidth - innerwidth, scrollheight + screenheight - innerheight + 100)

print(f"Sleeping for some time...")
sb.sleep(random.randint(5, 8))

Is there a better way?

python selenium-webdriver undetected-chromedriver chrome-devtools-protocol seleniumbase