硒webdriver Python在ubuntu SessionNotCreatedException

时间:2019-08-27 23:16:58

标签: python-3.x selenium-webdriver selenium-chromedriver

我试图在python3.6 / selenium3.141.0 / Ubuntu 18.04 LTS(Lightsail Amazon Linux)中创建一个webdriver对象,但是当我调用webdriver.Chrome()对象时,它引发了异常。当我使用Windows在本地运行该代码时,该代码有效(我只需要将chromedriver更改为Windows版本)即可。 chromedriver位于/ usr / bin / chromedriver中,我的用户有权执行它。

================================================ =========

我的代码是一个使用Flask并在gunicorn服务器上运行的Web应用,它使用python线程包。

谢谢!


在Ubuntu上安装的Chromedriver和Chrome版本: Chromedriver(http://chromedriver.storage.googleapis.com/index.html?path=76.0.3809.126/的chromedriver_linux64.zip)

命令:chromedriver -v;我得到它的版本: ChromeDriver 76.0.3809.126(d80a294506b4c9d18015e755cee48f953ddc3f2f-refs / branch-heads / 380 9 @ {#1024})

命令:google-chrome-stable -version;我得到它的版本: 谷歌浏览器76.0.3809.132

根据该站点,这些是76版本的正确/兼容版本: https://chromedriver.chromium.org/downloads 最新发行 如果您使用的是Chrome版本77,请下载ChromeDriver 77.0.3865.40 如果您使用的是Chrome版本76,请下载ChromeDriver 76.0.3809.126 如果您使用的是Chrome 75版,请下载ChromeDriver 75.0.3770.140 对于旧版的Chrome,请参阅以下有关支持它的ChromeDriver版本。

我的代码:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, UnexpectedAlertPresentException, TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import chromedriver_binary

##...(there is another function before to create folders)

def funcCX():
   chrome_options = webdriver.ChromeOptions()
   chrome_options.add_argument('--kiosk-printing')
   chrome_options.add_argument('--disable-gpu')
   chrome_options.add_argument('--headless')
   driver = webdriver.Chrome('/usr/bin/chromedriver', chrome_options=chrome_options)

这些是我用来安装chrome和chromedriver的命令:

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add 
echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list

sudo apt-get update 
sudo apt-get install google-chrome-stable
google-chrome-stable -version

sudo apt-get install -y unzip xvfb libxi6 libgconf-2-4

wget https://chromedriver.storage.googleapis.com/76.0.3809.126/chromedriver_linux64.zip
unzip chromedriver_linux64.zip

sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver
chromedriver -v

这是出现的错误消息:

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/connectionpool.py", line 
603, in urlopen
    chunked=chunked)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/connectionpool.py", line 
387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/connectionpool.py", line 
383, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ubuntu/AttCx1/pesquisas.py", line 127, in funcCiweb
    driver = webdriver.Chrome('/usr/bin/chromedriver', chrome_options=chrome_options)

  File "/home/ubuntu/att-env/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriv
er.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriv
er.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriv
er.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/selenium/webdriver/remote/webdriv
er.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/selenium/webdriver/remote/remote_
connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/selenium/webdriver/remote/remote_
connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/request.py", line 72, in 
request
    **urlopen_kw)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/request.py", line 150, in
 request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/poolmanager.py", line 326, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/util/retry.py", line 368, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/connectionpool.py", line 603, in urlopen
    chunked=chunked)
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/ubuntu/att-env/lib/python3.6/site-packages/urllib3/connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

由于另外两个线程试图使用相同的代码创建其他webdriver对象,此消息重复了两次。

1 个答案:

答案 0 :(得分:0)

我相信您应该在ChromeDriver初始化例程中添加下一行

 chrome_options.add_argument('--no-sandbox')

因为当您以root user的身份运行Chrome时,这是绝对必要的

参考文献: