LoginSignup
5
8

More than 5 years have passed since last update.

Python3 と Selenium Webdriver に挑戦

Last updated at Posted at 2017-07-01

きっかけ

仕事の中で行うテスト作業が長くて手間がかかるので、Python3の勉強も兼ねてやってみた

使用環境

システム名 対象バージョン・環境
OS macOS sierra 10.12.5
ブラウザ Chrome 59.0.3071.115
Python 3.6.1
Selenium 3.4.3
virtualenv 15.1.0
ChromeDriver 2.30

手順

基本は公式ドキュメントを参考にした

  1. virtualenvで環境を作成する場所を用意

    $ cd Desktop  
    $ mkdir PythonEnv
    $ cd PythonEnv
    
  2. 仮想環境を作成する

    $ virtualenv SeleniumPython
    $ cd SeleniumPython
    
  3. pipでseleniumをインストール

    $ pip install selenium
    Collecting selenium
      Downloading selenium-3.4.3-py2.py3-none-any.whl (931kB)
        100% |████████████████████████████████| 942kB 622kB/s 
    Installing collected packages: selenium
    Successfully installed selenium-3.4.3
    
  4. ChromeDriver - WebDriver for ChromeからChromeDriverをダウンロードする

  5. 解凍したファイルを/usr/local/binフォルダ配下に配置する(パスが通っていればなんでも良さそう)

    $ sudo mv chromedriver /usr/local/bin
    
  6. 作成した仮想環境に入る

    $ pwd
    /Users/<ユーザ名>/Desktop/SeleniumPython
    $ source bin/activate
    
  7. 以下のPythonファイルを作成する

    python_org_search.py
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome()
    driver.get("http://www.python.org")
    assert "Python" in driver.title
    elem = driver.find_element_by_name("q")
    elem.clear()
    elem.send_keys("pycon")
    elem.send_keys(Keys.RETURN)
    assert "No results found." not in driver.page_source
    driver.close()
    

作成したPythonファイルを実行する

$ python python_org_search.py

そうすると、Chromeが自動的に起動してPythonの公式ページが立ち上がる

今後

とりあえずは公式ドキュメントの2. Getting Startedをやってみようと思う

5
8
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
5
8