LoginSignup
7
1

More than 5 years have passed since last update.

Colaboratory上でSeleniumが使えないか試した(そしてダメだった)

Posted at

やりたかったこと

前回の続きとして、Beautiful SoupでHackMDへログイン状態でアクセスするためにSeleniumが使えないか試してみた。

やったこと

Firefoxのheadlessモードを試す

必要な物をインストール

!apt-get update
!apt-get install firefox
!wget https://github.com/mozilla/geckodriver/releases/download/v0.20.1/geckodriver-v0.20.1-linux64.tar.gz
!tar -zxvf geckodriver-v0.20.1-linux64.tar.gz
!cp ./geckodriver /usr/local/bin
!pip install selenium

コード

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver import FirefoxOptions

opts = FirefoxOptions()
opts.add_argument("--headless")
browser = webdriver.Firefox(firefox_options=opts)

browser.get('http://www.google.com')
print(driver.page_source)
browser.quit()

結果

WebDriverException: Message: Process unexpectedly closed with status signal
!cat geckodriver.log

1526799277910   geckodriver INFO    geckodriver 0.20.1
1526799277915   geckodriver INFO    Listening on 127.0.0.1:49500
1526799278918   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "--headless" "-profile" "/tmp/rust_mozprofile.aNlqPETDAbf4"
*** You are running in headless mode.
ExceptionHandler::GenerateDump cloned child 4713
ExceptionHandler::SendContinueSignalToChild sent continue signal to child
ExceptionHandler::WaitForContinueSignal waiting for continue signal...

Chromeのheadlessモードを試す

必要な物をインストール

!wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
!dpkg -i google-chrome-stable_current_amd64.deb

!wget https://chromedriver.storage.googleapis.com/2.38/chromedriver_linux64.zip
!unzip chromedriver_linux64.zip
!cp chromedriver /usr/local/bin/

コード

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
executable_path="/usr/local/bin/chromedriver"
service_args=["--verbose", "--log-path=/content/chromedriver.log"]

driver = webdriver.Chrome(chrome_options=options, executable_path=executable_path, service_args=service_args)

# Googleのトップ画面を開く。
driver.get('https://www.google.co.jp/')

print(driver.page_source)

結果

WebDriverException: Message: Service /usr/local/bin/chromedriver unexpectedly exited. Status code was: -6

ログ出力設定をしているつもり…なのだがファイルは生成されてなかった。

7
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1