pywinauto access a nested submenue - python

In pywinauto I am trying to access a nested submenue item that contains.
The Path is Datei->Importieren->Vorlagen->comdirect Musterdepot (attached screenshot)
Any idea how I can get this running?
The code I use:
app = Application(backend = 'uia').connect(path = "PortfolioPerformance.exe")
app.Dialog.Anwendungsmenü.item_by_path('Datei->Importieren->Vorlagen->comdirect Musterdepot').click_input()
The error I receive:
AttributeError: 'NoneType' object has no attribute 'is_active'
Screenshot of Menu:

After spending some time I figured out the following behaviour:
Once I expanded the Datei Menu, the print_control_identifiers updated to include the submenues.
The following code will click the correct, but maybe there are nicer solutions out there:
app = Application(backend = 'uia').connect(path = PROCNAME)
dialog = app.dialog
#first open the Importieren submenu:
dialog.Anwendungsmenü.item_by_path('Datei->Importieren')
# then the submenue appears on top level menues, open the importieren menu:
dialog["Importieren"].item_by_path('Vorlagen->comdirect Musterdepot').select()

Related

PywinAuto - Excel Automation Can not click the button

I m making an Excel automation via pywinauto library. But there is a hard challange for me due to using Excel Oracle add-ins called Smartview.
I need to click 'Private Connections' button, however i can't find any little info in app.Excel.print_control_identifiers() Private Connections
So i tried to use inspector.exe for find ui element regarding private connections button, however i couldn't find any little solvetion inside of inspector.exe's result inspector's result
Then i used another program called UISpy, however i can only find private connection's pane inside of the program. UISpy's result
i tried to find an answer but i couldn't find out anything. So, can you help me to click here?
By the way here is my code :
import pywinauto
from pywinauto import application
from pywinauto.keyboard import send_keys
from pywinauto.controls.common_controls import TreeViewWrapper
program_path = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
file_path = r"C:\Users\AytugMeteBeder\Desktop\deneme.xlsx"
app = application.Application(backend="uia").start(r'{} "{}"'.format(program_path, file_path))
# sapp = application.Application(backend="uia").connect(title = 'deneme.xlsx - Excel')
time.sleep(7)
myExcel = app.denemeExcel.child_window(title="Smart View", control_type="TabItem").wrapper_object()
myExcel.click_input()
Panel = app.denemeExcel.child_window(title="Panel", control_type="Button").wrapper_object()
Panel.click_input()
time.sleep(1)
app.denemeExcel.print_control_identifiers()

How to read console promp in pyWinauto

I'd like to control MobaXterm app with Python and trying to find a way for the code to read and fetch the string from the promp in each tab (terminal).
from pywinauto.application import Application
app = Application(backend='uia').start('"C:\\Program Files (x86)\\Mobatek\\MobaXterm\\MobaXterm.exe"').connect(title='val',timeout=100)
textEditor = app.val.child_window(control_type="Tab").wrapper_object()
print(textEditor.Edit.get_line(0))
textEditor.type_keys("^{TAB 2}")
textEditor.type_keys("test",with_spaces = True)
I tried to use get_line above, but it gives the following error:
print(textEditor.Edit.get_line(0))
AttributeError: 'TabControlWrapper' object has no attribute 'Edit'
Can you help me to find a way?

Get checkbox state and click accordingly using pywinauto

When i use below pywinauto code, i am getting error saying
from pywinauto.controls.win32_controls import ButtonWrapper
checkbox = ButtonWrapper(app.Dialog.child_window(title='Has Balance',auto_id='HasBalanceCheckEdit',control_type='CheckBox').TCheckBox.WrapperObject())
print(checkbox.GetCheckState())
Error:
raise MatchError(items = name_control_map.keys(), tofind = search_text)
pywinauto.findbestmatch.MatchError: Could not find 'TCheckBox' in 'dict_keys(['Has Balance', 'Has BalanceStatic', 'Static'])'
Based on the output you provided, next level spec TCheckBox is not needed. Just use this code:
checkbox = app.Dialog.child_window(title='Has Balance', auto_id='HasBalanceCheckEdit', control_type='CheckBox').wrapper_object()
Explicit ButtonWrapper instantiation is also not necessary because the control type is already auto detected by .wrapper_object(). That's why control_type='CheckBox' in your child_window can find the control.
used import pywinauto.controls.uia_controls to resolve the above code.

How can I resolve an error in Targetpath?

I am developing a small script that creates a chrome shortcut.
I want to have Facebook as homepage when this shortcut is used so I thought I could just append "www.facebook.com" to the target variable that is fed to shortcut.Targetpath, but I get an error. Here is my code snippet
desktop = winshell.desktop()
path = os.path.join(desktop, "Chrome.lnk")
target = r'C:\Program Files\Google\Chrome\Application\chrome.exe ' '"www.facebook.com'
wDir = r"%ProgramFiles%\Google\Chrome\Application"
icon = r"%ProgramFiles%\Google\Chrome\Application\chrome.exe"
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target
shortcut.WorkingDirectory = wDir
shortcut.IconLocation = icon
shortcut.save()
and the IDE shows the following error:
File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 581, in __setattr__
raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr))
AttributeError: Property '<unknown>.Targetpath' can not be set.
How can I fix that?
First of all, Targetpath should contain an executable path, you shouldn't add the default homepage to that variable.
To have Chrome open a default webpage you should use the shortcut.Arguments which is what you're actually missing and you should fix shortcut.TargetPath.
So you should modify your code along the following lines:
target = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
arguments = r'https://www.facebook.com'
and then
shortcut.Targetpath = target
shortcut.Arguments = arguments
In my answer, I assume all the rest is correct. Maybe you will need to modify a bit target and wDir.

iTunes win32com Python - AddTrack not working

I've been using the following code to try and create a new playlist in iTunes and a song from the main library - its example code i've found but i keep getting the following error when it runs. I've had a look through the iTunes COM interface documentation and it seems that AddTrack is only available under IITLibraryPlaylist but all of the example code Ive found is as below. Can anyone help>
Error: AttributeError: win32com.ge_py.iTunes 1.13 Type Library.IITPlaylist instance at 0x34035192 object has no attribute 'AddTrack'
Python Code:
import win32com.client
itunes = win32com.client.gencache.EnsureDispatch ("iTunes.Application")
mainLibrary = itunes.LibraryPlaylist
tracks = mainLibrary.Tracks
playlist = itunes.CreatePlaylist("Sonic Jams")
song = tracks.ItemByName('Teen Age Riot')
playlist.AddTrack(song)
in C#
Cast to IITUserPlayList
IITUserPlaylist rclibrary = (IITUserPlaylist)itunes.LibrarySource.Playlists.ItemByName["name"];
rclibrary.AddTrack(item);
i managed to get it using this code if anyone else needs it.
playlist = win32com.client.CastTo(itunes.CreatePlaylist("New List"), 'IITLibraryPlaylist')
song = tracks.ItemByName('Silver Rocket')
playlist.AddTrack(song)

Categories