LoginSignup
10
9

More than 3 years have passed since last update.

Macにseleniumをインストールして、pythonでお試し利用する

Posted at

目的

Macにseleniumをインストールして、pythonでお試し利用した際の備忘録です

Selenium/Google Chrome/Google Chromeドライバをインストール

Seleniumをインストール

pip3 install selenium

Google Chromeをインストール

以下を参考にGoogle Chromeをインストール

https://www.google.com/chrome
MacにGoogle Chromeをインストールする方法

Google Chromeドライバをインストール

Google Chromeのバージョンを確認して、
Google Chromeと同じバージョンのドライバをインストールする

Google Chromeのバージョンを確認

Google Chromeメニュー → Google Chromeについて

(例)
Google Chrome は最新版です
バージョン: 81.0.4044.138(Official Build) (64 ビット)

Google Chromeと同じバージョンのドライバをインストール

(例)Google Chrome が、バージョン: 81.0.4044.138 の場合
$ pip3 install chromedriver-binary==81.0.4044.138
Successfully installed chromedriver-binary-81.0.4044.138.0
$ pip3 show chromedriver-binary
Name: chromedriver-binary
Version: 81.0.4044.138.0
Summary: Installer for chromedriver.
Home-page: https://github.com/danielkaiser/python-chromedriver-binary
Author: Daniel Kaiser
Author-email: daniel.kaiser94@gmail.com
License: MIT
Location: /anaconda3/lib/python3.7/site-packages
Requires: 
Required-by: 

実行

以下を参考にさせて頂き、「Chromeを起動して"Selenium"とGoogle検索して、Seleniumの公式サイトを開く。」というテストプログラムを実行する。

Selenium ChromeDriver & PythonをMacで動かす準備メモ

selenium-test.py
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import chromedriver_binary

# ブラウザを開く。
driver = webdriver.Chrome()
# Googleの検索TOP画面を開く。
driver.get("https://www.google.co.jp/")

# 検索語として「selenium」と入力し、Enterキーを押す。
search = driver.find_element_by_name('q') 
search.send_keys("selenium automation")
search.send_keys(Keys.ENTER)
# タイトルに「Selenium - Web Browser Automation」と一致するリンクをクリックする。
#element = driver.find_element_by_partial_link_text("SeleniumHQ Browser Automation")
#element = driver.find_element_by_link_text("WebDriver")
element = driver.find_element_by_partial_link_text("Selenium")
element.click()

# 5秒間待機してみる。
sleep(5)
# ブラウザを終了する。
driver.close()

実行する

$ python3 selenium-test.py

googleから、seleniumのページに自動で遷移して、5秒後にブラウザが閉じればOK

スクリーンショット 2020-05-17 17.45.46.png

FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

$ python3 selenium-test.py
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/anaconda3/lib/python3.7/subprocess.py", line 769, in __init__
    restore_signals, start_new_session)
  File "/anaconda3/lib/python3.7/subprocess.py", line 1516, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "selenium-test.py", line 7, in <module>
    driver = webdriver.Chrome()
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Chromeドライバをバージョンを確認しつつインストールしたが正しく使えていなかった。
importして解決

import chromedriver_binary

インストールされたら、PATHに/usr/local/lib/python3.7/site-packages/chromedriver_binary/を追加するか、Pythonのスクリプトの冒頭で、import chromedriver_binary すると良いかと思います。

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="lst-ib"]"}

$ python3 selenium-test.py
Traceback (most recent call last):
  File "selenium-test.py", line 12, in <module>
    driver.find_element_by_id("lst-ib").send_keys("selenium")
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="lst-ib"]"}
  (Session info: chrome=81.0.4044.138)

以下で検索窓を見つけられなかったようなので、

driver.find_element_by_id("lst-ib").send_keys("selenium")

以下に置き換えて解決

search = driver.find_element_by_name('q')   

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"SeleniumHQ Browser Automation"}

Traceback (most recent call last):
  File "selenium-test.py", line 18, in <module>
    element = driver.find_element_by_link_text("SeleniumHQ Browser Automation")
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"SeleniumHQ Browser Automation"}
  (Session info: chrome=81.0.4044.138)

以下の部分で"SeleniumHQ Browser Automation"を検索できなかったようなので、

element = driver.find_element_by_partial_link_text("SeleniumHQ Browser Automation")

今回は、find_element_by_partial_link_text("Selenium") として部分検索を行い解決

element = driver.find_element_by_partial_link_text("Selenium")

参考

Selenium ChromeDriver & PythonをMacで動かす準備メモ
Selenium automates browsers. That's it!
MacにGoogle Chromeをインストールする方法
Pythonの開発環境を用意しよう!(Mac)
seleniumで「Message: session not created: This version of ChromeDriver only supports Chrome version 75」のエラーが表示される場合の対処法
selenium Downloads
No such file or directory: 'chromedriver': 'chromedriver'の解決
seleniumが起動しない
Selenium ChromeDriver & PythonをMacで動かす準備メモ
Seleniumで要素を選択する方法まとめ
【Python】find_element_by_link_text・・・linkTextから要素を取得する
https://www.google.com/chrome

10
9
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
10
9