Python Pywinauto no control_identifiers or children in application - python

Hey everyone I'm having a hard time automating a specific application. Using pywinauto I want to automate clicking, keypresses, etc. to login and benchmark this application but for some reason I can't find any control identifiers for this application. Am I doing something wrong? I used this same method with task manager and other applications and this works fine.
Important Documentation:
Code Example
Class Doc
Code:
import pywinauto
app = pywinauto.application.Application()
window_handle = pywinauto.findwindows.find_windows(title = u'Name of application')
#print window_handle #makes sure to see if handle exists
window = app.window_(handle = window_handle[0])
print window.Children() #first approach
print app.top_window_()._ctrl_identifiers() #second approach
Output:
>>>[]
>>>{}
Has this happened to anyone before and has found a way around it? Should I just resort to using pywin32 instead? Thank you!

Turns out the application does have control identifiers I just needed to get past the first screen. Using window.TypeKeys("{TAB}{TAB}{ENTER}") I was able to navigate past the home screen with keypresses and into the actual application which had all the identifiers.
Useful Links if anyone encounters this problem.
What is a control Identifier?

Related

QT automation with pywinauto

I'm trying to automate a QT app using pywinauto, the problem is pywinauto seems unable to click or select most objects and when it does they seem to be completely random to the ones I specified. Even using functions such as "click_input" execute properly, but the item is never actually clicked. Pywinauto apparently does recognize the button I'm trying to click and even prints it as an "pywinauto.application.WindowSpecification" object. I've noticed that pywinauto has some trouble with QT applications so any help would be appreciated. Thank you.
anki_App = Application(backend="uia").start(anki_Path)
anki_App.top_window().wait("Visible") #Wait for Anki's sync to server
anki = anki_App.window(title_re=r".* - Anki", class_name="AnkiQt")
anki['Import File'].click_input()
Snippet from results of anki.print_control_identifiers() since the content is too large.
Button - 'Import File' (L1751, T875, R1867, B910)
['Import File', 'Import FileButton', 'Button6']
child_window(title="Import File", control_type="Button")
Edit: Made some recommended changes, but the problem still remains. The click event doesn't seem to bring any errors yet no "click" actually happens.
After asking around in Pywinsauto's github community I was able to solve it in the following way:
anki_App = Application(backend="uia").start(anki_Path)
anki_App.top_window().wait("Visible") #Wait for Anki's sync to server
anki = anki_App.window(title_re=r".* - Anki", class_name="AnkiQt")
anki['Browse'].click_input(coords=(anki['Browse'].rectangle().left -1, anki['Browse'].rectangle().top -1), absolute = True)
Problem was that the coordinates were wrong. They were pretty close though, so clicking the upper left corner was all that needed to be done. Again, many thanks to the user airelil for helping me figuring out the problem.
Link to the issue

Cannot find .exe with Pywinauto's find_window(title="program.exe")

Does anyone know the trick to pywinauto's find_window function? I am building an application with kivy, and trying to use pywinauto to bring an .exe to the foreground, using the following code:
SetForegroundWindow(find_window(title='program.exe'))
I simply want to identify a currently open .exe, and bring it to the foreground. I have looked here https://pywinauto.github.io/docs/code/pywinauto.findwindows.html and it seems "title=" is what I want.
Does anyone know how to point to the .exe with pywinauto?
I think title is for window title (i.e. "python - Cannot find..." in case of this tab), are you sure it not more like "process='program.exe'" ?
if it needs to be and int then its pid (process id) and you can use this to get process id by title:
import win32gui,win32process
def get_window_pid(title):
hwnd = win32gui.FindWindow(None, title)
threadid,pid = win32process.GetWindowThreadProcessId(hwnd)
return pid
Eventually have at this answer as it contains really nice class for getting windows Python Window Activation, i dont want to copy paste, but use it and then you can do:
w = WindowMgr()
w.find_window_wildcard(".*Hello.*")
w.set_foreground()
find_window is low level function I wouldn’t recommend to use.
The right thing is Application object connected to the target process. It can be used so:
from pywinauto import Application
app = Application(backend=“uia”).connect(path=“program.exe”)
app.WindowTitle.set_focus()
If you have several app instances, there is a Desktop object to walk all the windows in the system:
from pywinauto import Desktop
Desktop(backend=“win32”).window(title=“Window Title”, found_index=0).set_focus()
You referred to the old docs for version 0.5.4, the latest one is 0.6.4 with two backends available and many bug fixes. The Getting Started Guide link on the main page is a good source to learn the main concept.

Pywinauto find process window and focus it

My current situation is that I open a process, which opens in a random location (thats how this process works).
I have the process PID so I need to somehow focus this window, and move it to the center of my screen.
Im doing something wrong as I can't even set focus on that window... tried it with different apps and got the same result...
The way I select the window -
appl = pywinauto.application.Application()
appl.connect(process=824)
app_dialog = appl.top_window_()
app_dialog.Minimize()
app_dialog.Maximize()
##app_dialog.SetFocus() ##doesn't work aswell
##pywinauto.win32functions.SetForegroundWindow(app_dialog)## doesn't work
Thanks for reading :)
Can't say why it doesn't work with pywinauto...
Got it to work with win32gui as the answer here- Python Window Activation
long yet efficient ;)
Method app_dialog.set_focus() should work in pywinauto 0.6.2. If not, it might be a bug. Is your application publicly available somehow? I'd like to reproduce it on my side. Are you trying to activate a background window while you have modal dialog on top of it?
Second case is a wrong usage of SetForegroundWindow(...). It should give a handle, but you pass WindowSpecification object app_dialog. Right way is the following:
handle = app_dialog.wrapper_object().handle
pywinauto.win32functions.SetForegroundWindow(handle)

simple web browser with auto refresh by webkit python

I'm trying to develop a simple digital signage system with raspberry-pi computer. What I'm actually trying to do is open a web page (which carrying information to be displayed) in full screen mode and refresh this page at certain interval of time. I came across some web view codes in python using "gtk" and "webkit" I managed to open a url in full screen mode, but I dont know how to use reload function as per my needs. Please help me.
Besides the two ways that are already described using a cron job (not an elegant way) or using javascript, it is also possible to schedule the reload in the python script.
from gi.repository import Gtk, GLib
from gi.repository import WebKit2
class ReloadView:
def __init__(self):
window = Gtk.Window()
window.connect('delete-event',Gtk.main_quit)
self.view = WebKit2.WebView()
self.view.load_uri('http://example.net')
GLib.timeout_add_seconds(5, self.reload) #every 5 seconds
window.add(self.view)
window.fullscreen()
window.show_all()
def reload(self):
self.view.reload()
#self.view.reload_bypass_cache() for complete reload
return True
if __name__ == "__main__":
ReloadView()
Gtk.main()
Please check out this resource that seems to have some valid solutions for your problem: https://raspberrypi.stackexchange.com/questions/6981/auto-refresh-for-midori
These scheduled actions are usually handled by cron jobs, but there is nothing preventing you from writing your own simple action scheduler.
edit: alternatively if you control the website itself, you could set the website itself to refresh with a certain interval. The ways to do this are described here: How to reload page every 5 second?

pywebkitgtk: Headless + full DOM interaction possible?

I am currently using the Python Webkit DOM Bindings to interact with a website programmatically and that's working for me.
The only problem is that it insists on opening a GTK window to display the page. Has somebody figured out a way to prevent it from opening a window? I.e. to use it in a headless way?
I'm initializing the view like this:
wv = pywebkitgtk.WebView(1024, 768, url=url)
which implicitly opens the GTK window and then I have an onload event-handler to manipulate the DOM.
I first thought of subclassing WebView, but that's not possible because it is a compiled class.
Any other ideas?
I'm the developer responsible for pythonwebkit, and I have a great deal of expertise covering these areas across several platforms. Realistically, you really, really want a completely "headless" WebKit port. In pythonwebkit that actually shouldn't be too hard to do, as there are only three "entry point" functions (one for window, one for document, and one for XMLHTTPRequest).
Really, somebody should do a proper "completely headless" port of WebKit. There already is an example program which is pretty close in WebKit's source tree, maybe that will get you started.
I've been using PyQT. PyQTWebView runs on Webkit and works great. Check out Ghost.py to get started, or use PyQT's API directly. Runs fully headless, and supports a decently recent build of Webkit.
You could try using Xvfb. I like using the command line and setting my display manually, but if you don't like that you could use this: http://cgoldberg.github.io/xvfbwrapper/
Can you get a handle to the GTK window and then call window.hide()? Otherwise, you might just have to use the full Webkit library.
Create a window and add the webview there, and never show the window..
I have webviews running without showing them, and can call a show_all if I need to show them.
web_view = pywebkitgtk.WebView()
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
sw = gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
sw.add(web_view)
window.add(sw)
#window.show_all()

Categories

Resources