最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

undetected chromedriver - How to set window size in Selenium Base in CDP Mode? - Stack Overflow

programmeradmin0浏览0评论

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 :

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?

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:

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?

Share Improve this question edited Feb 3 at 18:17 sudoExclamationExclamation asked Feb 3 at 17:26 sudoExclamationExclamationsudoExclamationExclamation 8,80410 gold badges51 silver badges119 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Use sb.cdp.set_window_rect(x, y, width, height). (All 4 parameters need to be set.) Eg:

from seleniumbase import SB

with SB(uc=True, test=True) as sb:
    url = "https://bot-detector.rebrowser/"
    sb.activate_cdp_mode(url)
    sb.cdp.set_window_rect(10, 10, 1200, 700)
    sb.sleep(10)
发布评论

评论列表(0)

  1. 暂无评论