Chrome在Selenium docker中下载失败

2ic8powd  于 2023-11-17  发布在  Docker
关注(0)|答案(1)|浏览(143)

我试图下载一个文件,Selenium运行在docker上,并得到错误失败-权限不足as can be seen here, from VNC to selenium docker
使用selenium的python脚本在另一个docker中运行,我需要它使用selenium下载文件,通过一个docker卷将其传递到安装到它们的文件夹。
这就是我运行Selenium Docker的方式:

docker run -d  --network host  -v /home/exports:/home/seluser/downloads:rw  -p 4444:4444 --shm-size="2g" selenium/standalone-chrome:4.0.0-rc-1-prerelease-20210804

字符串
这就是我使用selenium运行Python脚本的方式:

docker run --network host -v /home/logs:/logs -v /home/exports:/exports -i -t automated_testing


以下是python脚本中的Chrome选项:

chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--no-proxy-server')
    chrome_options.add_argument('--disable-gpu')      
    chrome_options.add_argument('--safebrowsing-disable-download-protection') 
    chrome_options.add_argument("--disable-web-security")
    chrome_options.add_argument("--allow-running-insecure-content")
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("test-type")
    prefs = {
    "profile.default_content_settings.popups": False,
    "download.prompt_for_download" : False,
    "download.directory_upgrade":  True,
    "download.default_directory" : "/home/seluser/downloads",
    'profile.default_content_setting_values.automatic_downloads': True,
      "safebrowsing.enabled": False
    }
    chrome_options.add_experimental_option("prefs",prefs)
    driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',desired_capabilities=chrome_options.to_capabilities())
    
    # Set permissions
    driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': '/home/seluser/downloads'}}
    command_result = self.driver.execute("send_command", params)


在观察VNC时,我看到了下载的错误。
帮助将不胜感激!

kzmpq1sx

kzmpq1sx1#

当你在独立的docker容器中运行你的代码时,你不需要初始化远程驱动程序。你可以初始化chromedriver,不要使用localhost:4444。这应该在你从主机运行自动化脚本时使用。
尝试在你的主机上运行上面的自动化代码,而不是在Docker容器中触发它。

相关问题