【问题标题】:Python selenium - Not working in UbuntuPython selenium - 在 Ubuntu 中不起作用
【发布时间】:2018-08-25 09:09:16
【问题描述】:

下面是我在 Ubuntu 16.04 上运行的代码 sn-p。

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    path_to_chromedriver = '/usr/local/bin/chromedriver' # change path as needed
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument('--disable-extensions')
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox') 
    options.add_argument('--no-sandbox')
    service_log_path = "{}/chromedriver.log".format('/tmp/local/')
    service_args = ['--verbose']
    driver = webdriver.Chrome(executable_path = path_to_chromedriver,chrome_options=options,
            service_args=service_args,
            service_log_path=service_log_path)

不管我放什么标志,我总是在 chromedriver.log 文件中得到以下错误。

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

      } ]
   },
   "desiredCapabilities": {
      "browserName": "chrome",
      "goog:chromeOptions": {
         "args": [ "--headless", "--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_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.001][INFO]: Launching chrome: /opt/google/chrome/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --full-memory-crash-report --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.np7aOC/internal --logging-level=1 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12652 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.iUR4hY data:,
[1.001][DEBUG]: DevTools request: http://127.0.0.1:12652/json/version
[1.002][WARNING]: PAC support disabled because there is no system implementation
[927:927:0316/063553.173763:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

[1.037][DEBUG]: DevTools request failed
[1.087][DEBUG]: DevTools request: http://127.0.0.1:12710/json/version
[1.087][DEBUG]: DevTools request failed
[1.138][DEBUG]: DevTools request: http://127.0.0.1:12710/json/version
................................................................
................................................................
[61.031][INFO]: RESPONSE InitSession unknown error: Chrome failed to start: exited abnormally
[61.031][DEBUG]: Log type 'driver' lost 0 entries on destruction
[61.031][DEBUG]: Log type 'browser' lost 0 entries on destruction

我也尝试从非 root 用户运行它,但仍然出现同样的错误。

我只能通过 SSH 连接到这台机器。我试过了,如果在 screen 中运行命令有帮助但没有用,使用 crontab 也没有。

任何指导都会非常有帮助。

更新 1:

下面是重启后的chromedriver日志:

[1.003][INFO]: COMMAND InitSession {
   "capabilities": {
      "alwaysMatch": {
         "browserName": "chrome",
         "goog:chromeOptions": {
            "args": [ "start-maximized", "--disable-extensions", "--headless", "--disable-gpu", "--no-sandbox" ],
            "extensions": [  ]
         },
         "platformName": "any"
      },
      "firstMatch": [ {

      } ]
   },
   "desiredCapabilities": {
      "browserName": "chrome",
      "goog:chromeOptions": {
         "args": [ "start-maximized", "--disable-extensions", "--headless", "--disable-gpu", "--no-sandbox" ],
         "extensions": [  ]
      },
      "platform": "ANY",
      "version": ""
   }
}
[1.003][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_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.003][INFO]: Populating Local State file: {
   "background_mode": {
      "enabled": false
   },
   "ssl": {
      "rev_checking": {
         "enabled": false
      }
   }
}
[1.004][INFO]: Launching chrome: /opt/google/chrome/google-chrome --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --full-memory-crash-report --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.wpOJGk/internal --logging-level=1 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12725 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.XeSkzL data:,
[1.004][DEBUG]: DevTools request: http://127.0.0.1:12725/json/version
[1.008][WARNING]: PAC support disabled because there is no system implementation
[1752:1752:0321/084842.042837:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
[1.051][DEBUG]: DevTools request failed
[1.102][DEBUG]: DevTools request: http://127.0.0.1:12725/json/version
[1.102][DEBUG]: DevTools request failed
[1.152][DEBUG]: DevTools request: http://127.0.0.1:12725/json/version
[1.152][DEBUG]: DevTools request failed
[1.203][DEBUG]: DevTools request: http://127.0.0.1:12725/json/version

【问题讨论】:

    标签: python-2.7 selenium ubuntu selenium-webdriver selenium-chromedriver


    【解决方案1】:

    错误说明了一切:

    [455:455:0316/061123.770013:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
    [61.031][INFO]: RESPONSE InitSession unknown error: Chrome failed to start: exited abnormally
    

    错误堆栈跟踪清楚地提到您正在以 root 用户身份执行程序,而没有 --no-sandbox 参数。

    解决方案

    按照以下步骤操作:

    • 非 root 用户身份登录。
    • chromedriver 二进制文件复制到更易于访问的位置(例如 '/usr/local/bin/')并提供必要的权限 (chmod 777)
    • 使用以下代码块:

      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      
      path_to_chromedriver = '/path/to/chromedriver'
      options = Options()
      options.add_argument("start-maximized")
      options.add_argument("--headless")
      options.add_argument("--disable-gpu")
      options.add_argument("--disable-extensions")
      options.add_argument("--no-sandbox") 
      service_args = ['--verbose']
      service_log_path = '/tmp/local/chromedriver.log'
      driver = webdriver.Chrome(executable_path='/path/to/chromedriver', chrome_options=options, service_args=service_args, service_log_path=service_log_path)
      driver.get('http://google.com/')
      

    琐事

    Sandbox的故事


    更新 A

    因为您仍然看到错误:

    [927:927:0316/063553.173763:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
    

    尝试以下步骤:

    • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
    • 使用CCleaner 工具在执行测试套件之前和之后清除所有操作系统杂务。
    • 如果您的基础 Web Client 版本太旧,请通过 Revo Uninstaller 卸载它并安装最新的 GA 和发布版本的 Web Client
    • 执行您的@Test

    更新 B

    根据强制性的Requirements#1341 确保以下内容:

    • Chromedriver/usr/local/bin/
    • Chrome 浏览器/usr/bin/google-chrome

    关于[1.008][WARNING]: PAC support disabled,您可以放心地忽略该日志,因为它在Chromium 浏览器引擎 中按照PAC support disabled because there is no system implementation 的讨论进行了硬编码


    更新 C

    根据讨论 How to make firefox headless programatically in Selenium with python? 中的更新部分,您可以尝试 set_headless(headless=True) 方法作为替代方法,如下所示:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.set_headless(headless=True)
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get("http://google.com/")
    print ("Headless Chrome Initialized")
    driver.quit()
    

    另外看看讨论Running Chromedriver on Ubuntu Server headlessly

    【讨论】:

    • 正如我所说,我也尝试使用非 root 用户。我用另一个用户尝试了上面的代码,但它仍然给出同样的错误。
    • @pratibha 您能否使用更新的代码块和更新的错误堆栈跟踪更新问题以进行进一步分析?
    • 您有时间检查更新的代码和输出吗?
    • 我仍然可以看到[927:927:0316/063553.173763:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.。您可以按照我的答案更新执行cleanup 吗?
    • 它是一台 Ubuntu 机器,我只能通过 SSH 访问。我怎样才能在那里进行清理?
    猜你喜欢
    • 2018-11-25
    • 2017-05-13
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 2017-05-28
    相关资源
    最近更新 更多