如何以 root 身份运行 selenium chromedriver?(即使使用 --no-sandbox 也不起作用 [英] How to run selenium chromedriver as root? (not working even with --no-sandbox

查看:356
本文介绍了如何以 root 身份运行 selenium chromedriver?(即使使用 --no-sandbox 也不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行 chromedriver 来创建一些硒测试.我按照这个手册安装它.我正在尝试运行此代码:

I'm trying to run chromedriver to create some selenium tests. I followed this manual to install it. I'm trying to run this code:

from selenium import webdriver
driver = webdriver.Chrome(chrome_options=options)

当我以普通用户的身份调用这个 python 脚本时,它正在工作.但是当我将它称为 root 时(这对我来说是必需的),它不起作用.我尝试遵循一些建议,并且尝试使用多个 Google chrome 选项,例如:

When I call this python script as normal user, it's working. But when I call it as root (it is necessary for me), it's not working. I've tried to follow some advices and I was trying to use several Google chrome options, e.g.:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--no-sandbox')

driver = webdriver.Chrome(chrome_options=options,
                          service_args=[
                              '--verbose',
                              '--log-path=/home/me/Projects/selenium.log'
                          ]
)

但它仍然无法正常工作,这是日志的一部分:

But it's still not working and here is a part of log:

[1,001][INFO]: COMMAND InitSession {
   "capabilities": {
      "alwaysMatch": {
         "browserName": "chrome",
         "goog:chromeOptions": {
            "args": [ "--no-sandbox" ],
            "extensions": [  ]
         },
         "platformName": "any"
      },
      "firstMatch": [ {

      } ]
   },
   "desiredCapabilities": {
      "browserName": "chrome",
      "goog:chromeOptions": {
         "args": [ "--no-sandbox" ],
         "extensions": [  ]
      },
      "platform": "ANY",
      "version": ""
   }
}
[1,001][INFO]: Populating Preferences file: {
   "alternate_error_pages": {
      "enabled": false
   },
   "autofill": {
      "enabled": false
   },
   "browser": {
      "check_default_browser": false
   },
   "distribution": {
      "import_bookmarks": false,
      "import_history": false,
      "import_search_engine": false,
      "make_chrome_default_for_user": false,
      "show_welcome_page": false,
      "skip_first_run_ui": true
   },
   "dns_prefetching": {
      "enabled": false
   },
   "profile": {
      "content_settings": {
         "pattern_pairs": {
            "https://*,*": {
               "media-stream": {
                  "audio": "Default",
                  "video": "Default"
               }
            }
         }
      },
      "default_content_setting_values": {
         "geolocation": 1
      },
      "default_content_settings": {
         "geolocation": 1,
         "mouselock": 1,
         "notifications": 1,
         "popups": 1,
         "ppapi-broker": 1
      },
      "password_manager_enabled": false
   },
   "safebrowsing": {
      "enabled": false
   },
   "search": {
      "suggest_enabled": false
   },
   "translate": {
      "enabled": false
   }
}
[1,001][INFO]: Populating Local State file: {
   "background_mode": {
      "enabled": false
   },
   "ssl": {
      "rev_checking": {
         "enabled": false
      }
   }
}
[1,002][INFO]: Launching chrome: /opt/google/chrome/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-infobars --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --ignore-certificate-errors --load-extension=/tmp/.org.chromium.Chromium.3rN146/internal --log-level=0 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12613 --safebrowsing-disable-auto-update --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.org.chromium.Chromium.dOwaRR data:,
[1,002][DEBUG]: DevTools request: http://localhost:12613/json/version
[8523:8523:0216/145622.514842:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

奇怪的是,在 InitSession 命令中有 --no-sandbox 参数,但是当 Chrome 启动时,它没有.错误 Running as root without --no-sandbox is not supported. 反而出现了.

The strange is, that in the InitSession command there is --no-sandbox argument, but when the Chrome is launched, it's not. The error Running as root without --no-sandbox is not supported. shows up instead.

有什么建议吗?

推荐答案

问题出在 chromedriver 版本上.我使用了 2.26 版.重新安装到当前版本(2.35)后,它开始工作.因此,只有一个强制参数 --no-sandbox 并且现在一切正常.再次感谢.

The problem was in chromedriver version. I used version 2.26. After reinstallation to current version (2.35), it starts to work. So, there is only one mandatory argument --no-sandbox and everything is working now. Thank you again.

这篇关于如何以 root 身份运行 selenium chromedriver?(即使使用 --no-sandbox 也不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆