NicciBamf
NicciBamf

Reputation: 23

How can I select a menu item from a context menu with pywinauto?

I'm using pbixrefresher to automate refresh of a PBI report, which works perfrectly, however I also need to save a data table as a csv file, by copying the table into excel and then saving as csv since the table is to large to export as csv in PBI (circa 60k rows). I can get pywinauto to go to the data view in PBI and thanks to some help here have got the drop down menu (context menu or popup menu) to come up. I can tab down the menu to the required option but nothing happens when I use click_input('left'). The menu remains showing and no action is taken. Code as below:

import time
import os
from pywinauto.application import Application
from pywinauto import timings


timings.after_clickinput_wait = 1
WORKBOOK = "C:/BBSDMthly/DA_AgentList.pbix"
PROCNAME = "PBIDesktop.exe"
os.system('start "" "' + WORKBOOK + '"')
app = Application(backend = 'uia').connect(path = PROCNAME)
win = app.window(title = 'DA_AgentList - Power BI Desktop')
time.sleep(5)
win.wait("enabled", timeout = 300)
win.Save.wait("enabled", timeout = 300)
win.set_focus()
win.Data.click_input()
win.Save.wait("enabled", timeout = 300)
win.wait("enabled", timeout = 300)
win.click_input(button = 'right')
win.Save.wait("enabled", timeout = 300)
win.wait("enabled", timeout = 300)
win.set_focus()
win.type_keys("{VK_TAB 7}")
win.click_input(button='left')

I have searched for other queries on this and found several but none of the options I tried worked. For example, I tried:

app.PopupMenu.wait('visible', timeout=15).menu().get_menu_path('Copy table')[0].click_input()

which returned:

`File "C:\Program Files\Anaconda3\lib\site-packages\pywinauto\timings.py", line 375, in wait_until raise err

TimeoutError: timed out`

I also tried:

app.ContextMenu.child_window(title="Copy table", control_type="MenuItem").click_input()

which returned a MatchError:

MatchError: Could not find 'ContextMenu' in 'dict_keys(['DA_AgentList - Power BI DesktopDialog', 'Dialog', 'DA_AgentList - Power BI Desktop'])'

I also tried: app.top_window().menu.item_by_path('Copy table')[0].click_input()

which returned MatchError: Could not find 'Copy table' in 'dict_keys(['System'])'

I have tried using print_control_identifiers to get more information on the dropdown menu but nothing that is returned seems to relate to it. Unfortunately I don't have inspect.exe so can't use that to gain more information.

Can anyone help me on this please?

Upvotes: 1

Views: 2696

Answers (1)

NicciBamf
NicciBamf

Reputation: 23

I seem to have worked out how to select copy table from the drop down menu. I found inspect.exe as a standalone at GitHub and so managed to get it without any issues with admin rights. Using inspect.exe I got the coords for Copy table and used the code below to click on the button:

win.click_input(coords=(754,306))
win.Save.wait("enabled", timeout = 300)
win.wait("enabled", timeout = 300)
win.set_focus()

This has worked several times so seems to have solved my issue. Now all I need to do is work out how to paste the data into excel!

Upvotes: 1

Related Questions