我在 CDP 模式下使用SeleniumBase 。
我想知道如何在网站加载后在 CDP 模式下设置窗口大小?
如果我使用非 CDP 功能sb.set_window_size(x,y)
,那么它会被检测为在https://bot-detector.rebrowser.netruntimeEnableLeak
上打开 devtools的机器人:
我尝试过sb.cdp.set_window_size(x,y)
,但是该功能似乎不存在,因为它崩溃了:
sb.cdp.set_window_size(x,y)
AttributeError: 'types.SimpleNamespace' object has no attribute 'set_window_size'
编辑:我能够找到一种解决方法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))
有没有更好的方法?
使用
sb.cdp.set_window_rect(x, y, width, height)
。(所有 4 个参数都需要设置。)例如: