Запуск сервера Selenium с помощью sudo

У меня есть скрипт bash (назовем его selenium_server.sh), который запускает автономный сервер Selenium.

#!/bin/bash

java -Dwebdriver.chrome.driver=/usr/lib/chromium-browser/chromedriver -jar /home/username/selenium-server-standalone-3.6.0.jar -debug

Это работает просто отлично, но если я бегу sudo -u username ./selenium_server.sh, похоже, что он работает, но локальный веб-драйвер не может связаться с ним.

Чтобы быть понятным, пользователь username способен запускать сервер самостоятельно, но не через sudo,

Ниже вывод:

17:52:11.827 INFO - Selenium Server is up and running
2017-11-06 17:52:20.130:INFO:osjshC.ROOT:qtp1068824137-15: org.openqa.selenium.remote.server.WebDriverServlet-ae45eb6: Initialising WebDriverServlet
17:52:20.209 INFO - Binding default provider to: org.openqa.selenium.chrome.ChromeDriverService
17:52:20.210 INFO - Found handler: org.openqa.selenium.remote.server.commandhandler.BeginSession@3ec2006e
17:52:20.214 INFO - /session: Executing POST on /session (handler: BeginSession)
17:52:20.280 DEBUG - Memory-based payload for: {capabilities={firstMatch=[{}], alwaysMatch={browserName=chrome, platformName=any}}, desiredCapabilities={browserName=chrome, version=, platform=ANY}}
17:52:20.326 INFO - Mismatched capabilities. Creating a synthetic w3c capability.
17:52:20.336 INFO - Capabilities are: Capabilities {browserName=chrome, platformName=ANY, version=, platform=ANY, }
17:52:20.346 INFO - Capabilities {browserName=chrome, platformName=ANY, version=, platform=ANY, } matched class org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
17:52:20.355 INFO - Capabilities {browserName=chrome, platformName=ANY, version=, platform=ANY, } matched class org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
17:52:20.374 DEBUG - Waiting for [http://localhost:25238/status]
17:52:20.387 DEBUG - Polling http://localhost:25238/status
Starting ChromeDriver 2.32 on port 25238
Only local connections are allowed.
17:52:20.411 DEBUG - Polling http://localhost:25238/status
17:52:20.420 DEBUG - sun.net.www.MessageHeader@e869bdd5 pairs: {GET /status HTTP/1.1: null}{User-Agent: Java/1.8.0_131}{Host: localhost:25238}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}
17:52:20.424 DEBUG - sun.net.www.MessageHeader@4b8414694 pairs: {null: HTTP/1.1 200 OK}{Content-Length: 133}{Content-Type: application/json; charset=utf-8}{Connection: close}
17:52:20.853 DEBUG - CookieSpec selected: default
17:52:20.868 DEBUG - Auth cache not set in the context
17:52:20.873 DEBUG - Connection request: [route: {}->http://localhost:25238][total kept alive: 0; route allocated: 0 of 2000; total allocated: 0 of 2000]
17:52:20.901 DEBUG - Connection leased: [id: 0][route: {}->http://localhost:25238][total kept alive: 0; route allocated: 1 of 2000; total allocated: 1 of 2000]
17:52:20.906 DEBUG - Opening connection {}->http://localhost:25238
17:52:20.908 DEBUG - Connecting to localhost/127.0.0.1:25238
17:52:20.909 DEBUG - Connection established 127.0.0.1:58650<->127.0.0.1:25238
17:52:20.910 DEBUG - http-outgoing-0: set socket timeout to 10800000
17:52:20.910 DEBUG - Executing request POST /session HTTP/1.1
17:52:20.910 DEBUG - Target auth state: UNCHALLENGED
17:52:20.910 DEBUG - Proxy auth state: UNCHALLENGED
17:52:20.912 DEBUG - http-outgoing-0 >> POST /session HTTP/1.1
17:52:20.912 DEBUG - http-outgoing-0 >> Content-Type: application/json; charset=utf-8
17:52:20.912 DEBUG - http-outgoing-0 >> Content-Length: 173
17:52:20.912 DEBUG - http-outgoing-0 >> Host: localhost:25238
17:52:20.912 DEBUG - http-outgoing-0 >> Connection: Keep-Alive
17:52:20.912 DEBUG - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_131)
17:52:20.912 DEBUG - http-outgoing-0 >> Accept-Encoding: gzip,deflate
17:52:20.913 DEBUG - http-outgoing-0 >> "POST /session HTTP/1.1[\r][\n]"
17:52:20.913 DEBUG - http-outgoing-0 >> "Content-Type: application/json; charset=utf-8[\r][\n]"
17:52:20.913 DEBUG - http-outgoing-0 >> "Content-Length: 173[\r][\n]"
17:52:20.914 DEBUG - http-outgoing-0 >> "Host: localhost:25238[\r][\n]"
17:52:20.914 DEBUG - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
17:52:20.914 DEBUG - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_131)[\r][\n]"
17:52:20.914 DEBUG - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
17:52:20.915 DEBUG - http-outgoing-0 >> "[\r][\n]"
17:52:20.915 DEBUG - http-outgoing-0 >> "{"capabilities":{"firstMatch":[{}],"alwaysMatch":{"browserName":"chrome","platformName":"any"}},"desiredCapabilities":{"browserName":"chrome","version":"","platform":"ANY"}}"

Поскольку это связано с сервером, я оставил код драйвера до конца. Вот MWE.

#!/usr/bin/env python3

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4444/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME)

driver.get('https://stackru.com')
driver.close()

0 ответов

Другие вопросы по тегам