LoginSignup
2
1

More than 5 years have passed since last update.

Seleniumのブラウザを非表示(Xvfb)

Last updated at Posted at 2018-09-12

これまでXvfbを別途立ち上げて、DISPLAY=で指定していたが、pythonから直接Xvfbを起動終了する方法を知りました。

sudo apt install -y xvfb
pip3 install xvfbwrapper
from selenium import webdriver

# Xvfbの準備
from xvfbwrapper import Xvfb
display = Xvfb()
display.start()

# FirefoxがXvfb上で起動するため画面には映らない。
driver = webdriver.Firefox()
driver.get('https://www.google.co.jp')

# きちんと表示できているかをtitle値を確認
print(driver.title)

# ブラウザを閉じる
driver.quit()

# Xvfbを終了
display.stop()
2
1
0

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
2
1