1
1

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.

pywinautoでfirefoxをクリック

Last updated at Posted at 2019-06-18

目標

firefox の自動操作
今回は pywinauto を用いて右クリックをするところまで実行してみる

参考サイト

メモ帳を用いた基本操作: https://githubja.com/pywinauto/pywinauto
pywinautoリファレンス: https://pywinauto.readthedocs.io/en/latest/HowTo.html
pywinautoリファレンス(hwndwrapper): https://pywinauto.readthedocs.io/en/latest/code/pywinauto.controls.hwndwrapper.html

事前準備

handle を指定してfirefox を取得するため、swapyを利用
https://pywinauto.github.io/pages/swapy-is-a-pywinauto-inspector-and-code-generator.html

コード

right_click_firefox.py
from pywinauto.application import Application

firefox = Application().connect(handle=2951732)   # firefoxを起動後、swapyを見てhandleを指定
dlg = firefox.window(handle=2951732)            # 現在開いているタブをhandleを指定して取得
dlg.click(button='right', coords(200,200))      # 現在開いているタブの座標(200,200)を右クリック

これでfirefox画面上に右クリックをしたときの表示が出れば成功

(2019/06/19 追記)
dlgはpywinauto.base_wrapper.BaseWrapper に基づいているため、以下のコードでも可。
ただし、マウスがその地点に飛んでしまうので他の操作は出来なくなる。
リファレンス : https://pywinauto.readthedocs.io/en/latest/code/pywinauto.base_wrapper.html

right_click_firefox.py
from pywinauto.application import Application

firefox = Application().connect(handle=2951732)
dlg = firefox.window(handle=2951732)           
dlg.click_input(button='right', coords(200,200))      # click() から click_input() に変更

最後に

Application().connect() の方法はいくつかある。handleを調べなくてもできる方法もあるはず。

また、firefoxは現在開いているタブだけでなく、いくつかのウィンドウで構成されている

>>> firefox.windows()

で一覧を確認できる

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?