フロントエンドデベロッパーのメモ

スキル: HTML/Jade/Jinja2, CSS/SCSS, JavaScript(ES6), Angular, React,Next, Redux, Node, Express, Python, Flask, Postgres, Redis, MongoDB, Kafka, Knex, SQLAlchemy, React Native, EXPO, GraphQL/Apollo, REST, AWS, Heroku, Docker, CircleCI, SCRUM, XP, Vim, TDD

SeleniumとChromedriverを使ってみた

web developerのJD見てるとわりとSeleniumの使用経験について問われてることが多かったこともあり、前から気になっていました。 Pythonで書きたかったのでSeleniumPython bindingsをインストールしました。 まず最初は pip install selenium しましたが、ここで早速詰まって There was a problem confirming the ssl certificate: とエラーが吐かれてました。 pipのバージョンに関する問題かななどと調べて見ましたがイマイチ適切な原因が見つからなかったのでファイルダウンロードすることにしました。

pypi.org

そしてwebdriverのインストール、webdriverはブラウザと同じpathにないといけない点を気をつけないといけません。 僕の場合は/usr/bin/の直下でした。 ただwebdriverのインストールした時も問題が。 mvコマンド使ってdriverファイルを移動させようとしたものの

mv: rename chromedriver to ../../../usr/bin/chromedriver: Operation not permitted

のメッセージが。 どうやらセキュリティ機能によってSystem Integrity Protectionが発動したことが原因でした。 この辺は「system integrity protection disable」とかググったらmacのサポーターたちが丁寧な回答を出してくれています。 ここまできたらあとは例を試すことができました。

import time
from selenium import webdriver

# Optional argument, if not specified will search path.
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get('http://www.google.com/xhtml')
time.sleep(5)  # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)  # Let the user actually see something!
driver.quit()

を走らせた時に自動でブラウザが起動して、検索窓にChromeDriverを打ち込み検索結果を表示その5秒後にブラウザ閉じるという動作をさせますが 今までしたことができるようになるのはやはり楽しいし感動しますね! しばらくSeleniumで遊んでみます。

Reference:

GitHub - SeleniumHQ/selenium: A browser automation framework and ecosystem.

Selenium Client Driver — Selenium 3.14 documentation

Downloads - ChromeDriver - WebDriver for Chrome

system integrity protection disable - Google 検索