首页 > 解决方案 > chrome驱动程序不会使用python保持最大化

问题描述

使用 python 的 Chromedriver v88.0.4324.96 和 88.0.4324.27 不会保持最大化。我的旧 groovy 代码仍然可以使用相同的驱动程序(和选项),所以我去尝试操作这些 chromeoptions,但没有成功:

    options.add_argument("--window-size=1920x1080")
    options.add_argument("--start-maximized")
    options.add_argument('--disable-extensions')
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-gpu")
    options.add_argument("--disable-features=NetworkService")
    options.add_argument("--disable-features=VizDisplayCompositor")
    options.add_argument("--no-sandbox")
    options.add_argument("--useAutomationExtension=false")

在更新到 v.85 后,我已经查看了 Python can't fullscreen in webdriver chrome 但是全屏的东西给出了相同的结果,并且该.maximize_window()函数在窗口打开后立即杀死它:(这似乎是一个流行的解决方案。

我也无法升级到最新的 chrome(策略)并且查看发行说明问题 3690 可能是问题,但现在https://peter.sh/experiments/chromium-command-line-switches/已关闭...

查看 chromedriver 日志,我看到调整大小发生了,但不是来自我:

[1614679235.268][INFO]: Waiting for pending navigations...
[1614679235.268][DEBUG]: DevTools WebSocket Command: Runtime.evaluate (id=144) 0C0CC2C41CA4BE07557A5F9CB5CFFFEC {
   "expression": "1"
}
[1614679235.269][DEBUG]: DevTools WebSocket Response: Runtime.evaluate (id=144) 0C0CC2C41CA4BE07557A5F9CB5CFFFEC {
   "result": {
      "description": "1",
      "type": "number",
      "value": 1
   }
}
[1614679235.269][INFO]: Done waiting for pending navigations. Status: ok
[1614679235.269][DEBUG]: DevTools WebSocket Command: Browser.getWindowForTarget (id=6) browser {
   "targetId": "0C0CC2C41CA4BE07557A5F9CB5CFFFEC"
}
[1614679235.269][DEBUG]: DevTools WebSocket Response: Browser.getWindowForTarget (id=6) browser {
   "bounds": {
      "height": 935,
      "left": 10,
      "top": 10,
      "width": 1920,
      "windowState": "normal"
   },
   "windowId": 1
}

标签: pythonseleniumwebdriverselenium-chromedriver

解决方案


每次都是截图

s = lambda x: self.driver.execute_script('return document.body.parentNode.scroll' + x) self.driver.set_window_size(s('Width'), s('Height'))

基本上,每次截图时不要设置窗口位置


推荐阅读