LoginSignup
0
0

More than 1 year has passed since last update.

Seleniumの基本的な使い方メモ

Last updated at Posted at 2021-08-11

はじめに

  • Seleniumは、ブラウザ操作を自動化するツール。
  • Chrome を起動してGoogleのトップページの検索で「Python」を検索し、Chrome を閉じます。

環境

  • OS:Windows10 64bit
  • 検証日:2021/08/12
  • Anaconda:2021.05
  • python:3.8.8
  • selenium:3.141.0
  • Google Chrome:92.0.4515.131(Official Build) (64 ビット)
  • ChromeDriver:92.0.4515.107

サンプルコード

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#Chrome を動かすドライバを読み込む
driver_path = "G:\home2021\Python\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(driver_path) 
url = 'https://www.google.com/'
#Chrome を起動
driver.get(url)
#方法1:検索テキストボックスの要素をClass属性名から取得
search_box = driver.find_element_by_class_name("gLFyf")

#方法2:検索テキストボックスの要素をName属性から取得
#search_box = driver.find_element_by_name("q")

#方法3:検索テキストボックスの要素をタグ名から取得
#search_box = driver.find_element_by_tag_name('input')
#検索テキストボックスに入力
search_box.send_keys('python')
#方法1:Enterキーを押下して検索を実行
search_box.send_keys(Keys.ENTER)

#方法2:フォーム内のDOMをたどってフォーム要素を自動でsubmit
#search_box.submit()
time.sleep(1)
driver.quit()

参考

テスト自動化ツール「Selenium」のインストール・APIの早見表サイト
Pythonでブラウザ操作を自動化するSelenium WebDriverの使い方

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