0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WSL2でseleniumを使ってみる

Last updated at Posted at 2021-12-05

chromeのインストール

$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
$ sudo apt install ./google-chrome-stable_current_amd64.deb

バージョンを調べる。

$ google-chrome --version
Google Chrome 96.0.4664.45

Chromedriverをインストール

chromeのバージョンの96.0.4664.45と同じものをインストールする。

pip install chromedriver-binary==96.0.4664.45.0

seleniumのインストール

pip install selenium

コードを実行する

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import chromedriver_binary
import lxml.html

# ブラウザーを起動
options = Options()
options.binary_location = '/usr/bin/google-chrome'
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

# サイトにアクセス
driver.get(
    'https://www.selenium.dev/ja/documentation/')

# htmlを取得・表示
data = driver.page_source
data = lxml.html.fromstring(data)
data = data.xpath("//h1")[0].text
print(data)

# ブラウザーを終了
driver.quit()

実行するとサイトのh1タグの内容が表示されます。

$ python main.py
Seleniumブラウザー自動化プロジェクト

参考にしたサイト

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?