LoginSignup
17
19

More than 1 year has passed since last update.

PythonでSeleniumライブラリーを使って、Webブラウザー操作を自動化!

Posted at

はじめに

Pythonプログラムで即業務に役立つサンプルプログラムとして、Webブラウザーの操作をPythonのSeleniumライブラリーを使って自動化する方法をご紹介します。
これによって、いわゆるRPA(Robotics Process Automation)を無料で構築できます。

動作環境

Visual Studio Code
Python3.8.8

各種利用ライブラリー

Selenium 3.141.0
chromedriver.exe ※
※実行環境のパソコンにインストールされているGoogleChromeのバージョンにマッチしたchromedriver.exeをダウンロードしてPythonプログラムと同一フォルダに保存する必要があります。(詳しくはYouTube動画で解説しています)

処理概要:

PythonでSeleniumを使って、YahooのホームページやGoogle検索、GoogleMapから住所や地図の自動取得ができるサンプルをご紹介します。

PythonでWeb画面を自動化_040.png

YouTubeでの解説:

プログラムの詳細やGoogleDriverのダウンロード方法、さらには、SeleniumIDEという便利なGoogleChromeの拡張機能の設定、使い方をYoutubeで詳しく解説していますので、ぜひ、ご覧ください。
https://youtu.be/H9RHthvL7_k

サンプルソース

YouTubeで紹介している処理のプログラムソースです。

1.YahooのTopページからファイナンスをクリックする操作:

Step1.py
from selenium import webdriver
import time
from selenium.webdriver.common.by import By

# driver = webdriver.Chrome('C:/200_Python/140_Webブラウザー操作(Selenium)/chrome') 
driver = webdriver.Chrome()
driver.get('https://www.yahoo.co.jp/')
time.sleep(2)
element = driver.find_element(By.LINK_TEXT, "ファイナンス")
element.click()
time.sleep(5)
driver.quit()

2-1.Google検索で検索結果を取得する:

Step2-1.py
from selenium import webdriver
import time
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.google.com/')
time.sleep(3)

element = driver.find_element(By.NAME, "q")
element.send_keys("python")
element.submit()
time.sleep(2)
for element in driver.find_elements_by_xpath('//h3[@class="LC20lb MBeuO DKV0Md"]'):
    print(element.text)

time.sleep(5)
driver.quit()

2-2.Google検索で検索結果を取得する(ヘッドレスモード(ブラウザーを表示しない)):

Step2-2.py
from selenium import webdriver
import time
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument('--headless')

driver = webdriver.Chrome(options=options)
driver.get('https://www.google.com/')
time.sleep(3)

element = driver.find_element(By.NAME, "q")
element.send_keys("python")
element.submit()
time.sleep(2)
for element in driver.find_elements_by_xpath('//h3[@class="LC20lb MBeuO DKV0Md"]'):
    print(element.text)

time.sleep(5)
driver.quit()

3.地名一覧エクセルをインプットにGoogleMapで住所と地図画像を取得する:

詳細は、YouTube動画も見てね!!
https://youtu.be/H9RHthvL7_k

Step3.py
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import pandas as pd

df = pd.read_excel('地図調査一覧.xlsx') 

driver = webdriver.Chrome()
driver.get('https://www.google.co.jp/maps/')
time.sleep(3)

for i, r in df.iterrows():
    driver.find_element(By.ID, "searchboxinput").click()
    driver.find_element(By.ID, "searchboxinput").send_keys(r['地名'])
    driver.find_element(By.ID, "searchboxinput").send_keys(Keys.ENTER)
    time.sleep(2)
    df.loc[i,'住所'] = driver.find_element(By.CSS_SELECTOR, ".RcCsl:nth-child(3)").text
    driver.save_screenshot(r['地名']+'.png')
    driver.find_element(By.ID, "searchboxinput").clear()

df.to_excel('地図調査一覧.xlsx', index=None) 

time.sleep(5)
driver.quit()

最後に:

今後も、業務に役立ちそうなプログラムを作成して掲載していきたいと思います。

17
19
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
17
19