前提・実現したいこと
現在、私は「pythonにてseleniumを起動し、ヘッドレスモードでfirefoxを立ち上げ、そしてあるサイトにアクセスしてキャプチュアを取る」ということを実現したいと思っています。
しかし、firefoxを立ち上げるところでトラブルが発生し、困っております。ご助言のほど、宜しくお願い致します。
環境
ConoHaのVPS(CentOS7)
Python(3.7.3)
firefox(60.9.0)
selenium(3.141.0)
geckodriver(0.24.0)
数日ほど前にVPSを契約し、同じ日に上記のソフトをインストールしたので最新バージョンになっていると、認識しています。
ちなみにPythonとgeckodriverのインストールされた場所は以下の通りです。
/usr/local/bin/python3.7
/usr/local/bin/geckodriver
発生している問題・エラーメッセージ
Traceback (most recent call last): File "test.py", line 12, in <module> browser = webdriver.Firefox() File "/home/yoshikawa/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__ keep_alive=True) File "/home/yoshikawa/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) File "/home/yoshikawa/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/home/yoshikawa/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/home/yoshikawa/.local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: connection refused
最後の行の「connection refused」が特に気になります。
しかし、実はこの質問を投稿する前日に同じプログラムを実行したところ、問題なく作動していました。そのため、なおさらなぜ動かないのかわかりません・・・。
該当のソースコード
# -*- coding: utf_8 -*- from pyvirtualdisplay import Display from selenium import webdriver # Set screen resolution to 1366 x 768 like most 15" laptops display = Display(visible=0, size=(1366, 768)) display.start() print("get started!!") # now Firefox will run in a virtual display. browser = webdriver.Firefox() # Sets the width and height of the current window browser.set_window_size(1366, 768) # Open the URL browser.get('http://www.vionblog.com/') # set timeouts browser.set_script_timeout(30) browser.set_page_load_timeout(30) # Take screenshot browser.save_screenshot('vionblog.png') print(browser.title) print(browser.current_url) print(browser.page_source) # quit browser browser.quit() # quit Xvfb display display.stop() print("finished!!")
試したこと
補足情報(FW/ツールのバージョンなど)
仮想ディスプレイでのヘッドレスモードですので、pyvirtualdisplayモジュールは既にインストールしています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/31 07:45