Memo

メモ > 技術 > 開発: Selenium > Python版Selenium環境をWindowsに構築

Python版Selenium環境をWindowsに構築
Python で Selenium WebDriver を使ったブラウザテストをする方法 http://akiyoko.hatenablog.jp/entry/2014/04/29/231343 Selenium IDEをインストール Firefoxで https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/ にアクセスしてインストール Selenium IDE を立ち上げた状態でブラウザを操作すると、コマンドが記録されていった そのままエクスポート
C:\localhost\home\python\public_html\selenium>python test.py
エクスポートされたファイルを実行してもエラーになった。Firefoxのバイナリを見つけられないらしい WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line FirefoxBinary を読み込んで、Firefoxの実行ファイルを直接指定
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary ←追加 def setUp(self): binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe') ←追加 self.driver = webdriver.Firefox(firefox_binary=binary) ←修正
# -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoAlertPresentException import unittest, time, re import os class Test(unittest.TestCase): def setUp(self): binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe') self.driver = webdriver.Firefox(firefox_binary=binary) self.driver.implicitly_wait(30) self.base_url = "http://www.yahoo.co.jp/" self.verificationErrors = [] self.accept_next_alert = True def test_(self): driver = self.driver driver.get(self.base_url) driver.find_element_by_id("srchtxt").clear() driver.find_element_by_id("srchtxt").send_keys(u"テスト") driver.find_element_by_id("srchbtn").click() driver.find_element_by_link_text(u"テスト - Wikipedia").click() driver.find_element_by_link_text(u"ソフトウェアテスト").click() time.sleep(3) FILENAME = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ss\\screen.png") # Get Screen Shot driver.save_screenshot(FILENAME) def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except NoSuchElementException as e: return False return True def is_alert_present(self): try: self.driver.switch_to_alert() except NoAlertPresentException as e: return False return True def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": unittest.main()
これで実行するとOKだった…が、最低限のものなら、もっとシンプルに書ける http://qiita.com/setsulla/items/918ea1872c1947bf108f
# -*- coding: utf-8 -*- import os, time from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary FILENAME = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ss/screen.png") binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe') driver = webdriver.Firefox(firefox_binary=binary) driver.get("http://www.yahoo.co.jp/") driver.find_element_by_id("srchtxt").clear() driver.find_element_by_id("srchtxt").send_keys(u"テスト") driver.find_element_by_id("srchbtn").click() time.sleep(3) # Get Screen Shot driver.save_screenshot(FILENAME) # Close Web Browser driver.quit()

Advertisement