selenium :Docker容器中的 chrome :截图?

vojdkbi0  于 2023-02-01  发布在  Go
关注(0)|答案(1)|浏览(184)

我使用的是官方的 selenium chrome 合金驱动程序图片:https://hub.docker.com/r/selenium/standalone-chrome/
不幸的是,我无法在容器中进行屏幕截图。
这段代码在容器外完全可以正常工作:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://www.python.org")
driver.save_screenshot("some_file.png")
driver.close()

但是在container里面保存_screenshot返回false,我在chrome的github里面发现了一个小提示,screensize和depth必须设置,但是设置成1920 * 1080 * 24并没有改变什么

zdwk9cvp

zdwk9cvp1#

如果您使用容器,那么您应该考虑使用完全分离的浏览器容器。Selenoid(或其Kubernetes版本Moon)利用Selenium Grid机制来实现此目的。这意味着您的代码中唯一需要做的事情就是指定Selenium Hub服务器和所需的浏览器版本。

capabilities = {
        "browserName": "chrome",
        "version": "109.0"
    }
    context.driver = webdriver.Remote(
        command_executor="http://localhost:4444/wd/hub",
        desired_capabilities=capabilities)

远程驱动程序完全支持从任何浏览器截图,您无需担心维护浏览器驱动程序和浏览器。
官方网页:https://aerokube.com/selenoid/latest/

相关问题