首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ubuntu SessionNotCreatedException上的Selenium

ubuntu SessionNotCreatedException上的Selenium
EN

Stack Overflow用户
提问于 2019-08-27 23:16:58
回答 1查看 1.9K关注 0票数 0

我试图在python3.6/selenium3.141.0/Ubuntu18.04LTS (Lightsail )中创建一个webdriver对象,但是当我调用webdriver.Chrome()对象时,它会引发一个异常。当我使用windows在本地运行它时,代码可以工作(我只需要将chromedriver更改为windows版本)。chromedriver位于/usr/bin/chromedriver中,我的用户有执行它的权限。

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

我的代码是一个web应用程序,使用的是烧瓶,运行在gunicorn服务器上,它使用python线程包。

提前感谢!

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

命令: chromedriver -v;我得到它的版本: ChromeDriver 76.0.3809.126 ChromeDriver 9@{#1024})

命令: Google - Chrome稳定的-version;我得到它的版本:GoogleChrome76.0.3809.132

根据本网站,这是76版的正确/兼容版本:https://chromedriver.chromium.org/downloads当前版本如果使用ChromeDriver 77,请下载ChromeDriver 77.0.3865.40 (如果使用ChromeDriver 76 ),请下载ChromeDriver 76.0.3809.126 (如果使用ChromeDriver 75 ),请下载ChromeDriver 75.0.3770.140 (旧版),请参阅下面的支持Chrome的ChromeDriver版本。

我的代码:

代码语言:javascript
运行
复制
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的命令:

代码语言:javascript
运行
复制
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

这是出现的错误消息:

代码语言:javascript
运行
复制
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对象,此消息重复了两次。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-29 09:38:13

我认为您应该在ChromeDriver初始化例程中添加下一行。

代码语言:javascript
运行
复制
 chrome_options.add_argument('--no-sandbox')

因为当您将Chrome作为根用户运行时,这是绝对必要的

参考文献:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57683250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档