python - Ubuntu : selenium. common.exceptions.SessionNotCreatedException 上的 Chromedriver:消息:未创建 session

标签 python amazon-web-services ubuntu amazon-ec2 selenium-chromedriver

我在 AWS(EC2 实例)的 Ubuntu 环境中遇到 Selenium + Chromedriver 的问题。
我正在使用 Chromedriver Linux64 版本( wnload chromedriver for Linux: wget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip )。然后我将 Chromedriver 放入 /usr/bin .
使用 sudo dpkg -i google-chrome-stable_current_amd64.deb 为 Ubuntu 下载 Chrome如果我使用 google-chrome --version 验证 chrome 的版本我看到它是:

Google Chrome 78.0.3904.70 
以下 Python 代码有效,但问题是它只是偶尔有效。
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--window-size=1420,1080')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")
options.add_argument('--disable-gpu')


driver = webdriver.Chrome(chrome_options=options)

#Set base url (SAN FRANCISCO)
base_url = 'https://www.bandsintown.com/en/c/san-francisco-ca?page='

#Build events array 
events = []
eventContainerBucket = []

for i in range(1,2):

    #cycle through pages in range
    driver.get(base_url + str(i))
    pageURL = base_url + str(i)
    print(pageURL)
虽然上面的代码在过去没有问题,但如果我运行它几次,我最终会收到以下错误:
    Traceback (most recent call last):
  File "BandsInTown_Scraper_SF.py", line 84, in <module>
    driver = webdriver.Chrome(chrome_options=options)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
  (Session info: headless chrome=78.0.3904.70)
我已经阅读了要解决此问题,您可能需要编辑 etc/hosts文件。我看过这里,一切看起来都很好:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 
我也可以通过服务器使用请求和访问 url。例如,以下文本没有给我任何问题:
url = 'https://www.bandsintown.com/en/c/san-francisco-ca?page=6'
res = requests.get(url)
html_page = res.content

soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find_all(text=True)
print(text)
我认为可能导致此问题的另一条重要信息是 Chromedriver 可能不允许在 headless 模式下运行。例如,如果我输入 chromedriver在终端中,我收到以下消息:
Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
最后,如果我尝试做 chmod 777/usr/bin ,上面写着 operation not permitted .这可能是问题的一部分吗?
因此,Chrome + Chromedriver 似乎是相同的版本,所以这不是问题。 Chromedriver 和 Selenium 似乎被阻止了。我有点困惑如何解决这个问题。

最佳答案

这样就解决了,问题似乎已经到了我的尽头。删除此行:options.add_argument("--remote-debugging-port=9222")似乎解决了这个问题。谢谢。

关于python - Ubuntu : selenium. common.exceptions.SessionNotCreatedException 上的 Chromedriver:消息:未创建 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673231/

相关文章:

python - Jupyter Notebook 执行在 Visual Studio 代码中变灰

ruby-on-rails - rbenv 权限在 Ubuntu 上被 Assetic 拒绝

ubuntu - 将 OpenCL.so 库添加到 makefile,我收到错误 "undefined reference to"

django - 制作 : *** [port-target] Error 2 When trying to install GDAL for GeoDjango using Ubuntu 18. 04

amazon-web-services - 结合使用AWS ECR和Portainer推/拉图像不起作用

amazon-web-services - 如何将 elasticache redis 集群设置为从属?

python - 将数据从正在运行的C代码链接到正在运行的Python代码

Python:使用 DictReader 和 DictWriter 将两个带有标题行和匹配值的 CSV 文件组合在一起

python - char* 的参数转换为 python 以通过 boost.python 在 C++ 中调用 python 函数

amazon-web-services - 如何在AWS s3和AWS ec2之间传输文件