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