19
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonからSeleniumでFirefoxを操作し画面キャプチャを保存する

Last updated at Posted at 2014-08-27
  • Seleniumで画面キャプチャを撮ると、縦長WEB画面も保存できるので便利。
  • MacのFirefoxのSeleniumIDEでエクスポートしたselenium.pyはwindowsに持っていても同様に動きました。

手順

環境準備

sudo easy_install selenium

Selenium IDEからエクスポート

1.Selenium IDEにてテストケースを作成。

スクリーンショット 2014-08-28 6.09.55.png

2.エクスポートでPython 2 / unitetest / WebDriverを選択

スクリーンショット 2014-08-28 6.10.35.png

3.selenium.py という名前で保存

selenium.pyを修正

このままでは画面キャプチャができない。

ERROR: Caught exception [ERROR: Unsupported command [captureEntirePageScreenshot | /Users/owner/a.jpg | ]]

という行の下に以下を追加

        driver.save_screenshot("a.jpg")

参考: Seleniumworks: Capture the Screen on Test failure - WebDriver

実行

python selenium.py

Firefoxが動いてa.jpgが生成されることを確認。

参考: selenium.py全文

selenium.py
# -*- 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.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Aa(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://auction.yahoo.co.jp/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_aa(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_link_text(u"マイ・オークション").click()
        driver.find_element_by_link_text(u"ヘルプ").click()
        driver.find_element_by_link_text(u"「IDまたはパスワードが違います」と表示される").click()
        # ERROR: Caught exception [ERROR: Unsupported command [captureEntirePageScreenshot | /Users/owner/a.jpg | ]]
        driver.save_screenshot("a.jpg")

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, 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()

basic認証への対応

selenium.py
- self.base_url = "http://secret.page.local/"
+ self.base_url = "http://username:password@secrete.page.local/"

xfce4のデスクトップアイコン対応

google.selenium
#!/bin/sh
python google.py

上記のようなものを用意しダブルクリックで実行すれば良い。

自動でfirefoxが終了しないようにする

google.py
+   #def tearDown(self):
+       #self.driver.quit()
+       #self.assertEqual([], self.verificationErrors)
19
16
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
19
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?