LoginSignup
0
2

More than 3 years have passed since last update.

Appiumで要素のスクリーンショットを取る.

Posted at

Appiumで要素のスクリーンショットを取る.

前回の記事で終わる予定だったが、困りごとが発生したので書いときます。
テストランナーを別のテスト対象で動かそうとして、出力画面を取ろうとしたらキャプチャーが取れなかった。

  • 環境
    Windows10
    Python 3.7
    Appium Python Client 1.1.0
    WindowsApplicationDriver 1.2.1

  • 原因 どうやら上記環境ではget_screenshot要素のキャプチャーはできないらしい

  • 上記リンク先にコードがあったがWindowsアプリ用に書いた。

題材はまたしてもワードパット

element.py
# -*- coding: utf-8 -*-
import cv2
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

~ 前回記事を参照ください~

if __name__ == '__main__':
  #Windows Appliction Driverを起動 . 
  #ワードパッドを起動しておく. 
  appdriver = SetUp_SelectClassName('WordPadClass')

  filepath = r"driver.png"
  #Driverの画面をキャプチャ.   
  appdriver.save_screenshot(filepath)
  #要素のを取得.
  element= appdriver.find_element_by_name("リッチ テキスト ウィンドウ")
  #位置とサイズを取り出す.
  elwindow = {}
  elwindow["top"] = element.location["y"]
  elwindow["bottom"] = element.location["y"]+element.size["height"]
  elwindow["left"] = element.location["x"]
  elwindow["right"] = element.location["x"]+element.size["width"]
  #保存したイメージから要素の画像を切り出す.  
  img = cv2.imread(filepath)
  img1 = img[elwindow["top"] : elwindow["bottom"] ,  elwindow["left"] : elwindow["right"] ]
  #確認してみる.  
  cv2.imshow('sample', img1)
  cv2.waitKey(0)
  cv2.destroyAllWindows()

これで要素のイメージを取って画像認識できました。
トリミングについてはこちらを参考にしました。

0
2
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
0
2