Pywinauto menu selection error - Wireshark - python

I am very new to windows automation.I am automating wireshark using autopy.Now i need to open a file wireshark.I dont have swapy tool.I also dot want to do it in pyshark.I just want to try only in pywinauto.so i tried this way:
from pywinauto import application
print("Starting the proogram")
app=application.Application()
app.start_(r"C:\Program Files\Wireshark\Wireshark.exe")
win = app.window_(title_re = ".*Wireshark Network Analyzer.*")
win.MenuSelect("File->Open")
But i get this below error :
pywinauto.findwindows.WindowNotFoundError
Kindly help me out here with this guys.Thanks in advance

As I can see WireShark is starting up several seconds. You need waiting main window longer than default timeout (5 sec.).
win.wait('ready', timeout=15)
"Software Update" window can also be handled if it pops up:
if app.SoftwareUpdate.exists(timeout=10):
app.SoftwareUpdate.SkipThisVersion.click()
app.SoftwareUpdate.wait_not('visible') # just to make sure it's closed
win.wait('ready', timeout=15)
EDIT (2019, Jan, 21): the latest version of WireShark is built on Qt5 and current pywinauto example is maintained in the repo: examples/wireshark.py.
(old part of the answer below)
But in any case pywinauto doesn't support GDK widgets (even Windows UI Automation API doesn't support GDK apps). Menu is not available to pywinauto or UIA-based tools. You can deal with WireShark using workarounds only like so:
win.type_keys('%F{ENTER}') # Alt+F, Enter (it calls "&File->&Open" menu)
app.WiresharkOpenCaptureFile.FilenameEdit.set_edit_text('I can set text here')
app.WiresharkOpenCaptureFile.Open.click()
app.WiresharkOpenCaptureFile.wait_not('visible')
"Open" dialog is standard variation of Windows Open/Save dialog and pywinauto supports many controls on it.
To check which dialog is supported well by pywinauto use print_control_identifiers() method:
win.print_control_identifiers() # prints nothing
app.WiresharkOpenCaptureFile.print_control_identifiers() # prints a lot of controls

Related

AutoKey: Switch to last active application like Alt-Tab by using AutoKey

is it possible to send the alt+tab for switch to last window with AutoKey ?
i tried without success:
keyboard.send_keys("<alt>+<shift>+<tab>")
Or forward window:
keyboard.press_key('<alt>')
keyboard.press_key('<tab>')
keyboard.release_key('<tab>')
keyboard.release_key('<alt>')
Or backward window:
keyboard.press_key('<alt>')
keyboard.press_key('<shift>')
keyboard.press_key('<tab>')
keyboard.release_key('<tab>')
keyboard.release_key('<shift>')
keyboard.release_key('<alt>')
result: no error but only moves the tab count inside the editor.
TL;DR: Not directly with our API.
The AutoKey API talks directly to the current active window. So, sending events targeted at the desktop (DTE) will only work if the current active window recognizes them as such and either forwards them to the DTE or emulates what they do.
However, since AutoKey scripts are written in full Python 3, if you can figure out how to do it yourself in Python, AutoKey can run it for you. And, if some other solution is available, you can run it from within an AutoKey script using the subprocess module.
Autokey's Window class allows you to activate a window by name (via wmctrl), among other functionality. Something in that class may be what you're looking for.

Send keys to background window/application (Python)

I'm currently working on a Selenium program that requires I open up a system file-selector dialog. Unfortunately it's impossible to circumvent this by just sending keys to a webpage attribute, as I have to select a button with no file-acceptance, which automatically opens up the file-selector dialog.
I believe the only solution is to send keys through the system itself to the file selector. Unfortunately, the method I'm currently using (below) requires that the window be active for it to receive the keys.
I used the pynput library in order to send the keys on my first iteration. The pynput documentation for keyboards can be found here:
https://pynput.readthedocs.io/en/latest/keyboard.html
from pynput.keyboard import Key, Controller
import os, time
file = "723583.jpg" #this is a local directory file
keyboard = Controller()
keyboard.type(os.path.abspath(file))
time.sleep(5) #Please ignore the bad style of using these sleeps
keyboard.press(Key.enter) #They're just for testing
time.sleep(3)
keyboard.press(Key.enter)
time.sleep(3)
On other Stackoverflow questions, I've found solutions for Windows computers (e.g. using win32), though I haven't been able to find anything for MacOS, which I'm currently using, or an equivalent multi-platform solution. Does anybody know how I might be able to send keys to a background application as such?

Tkinter activate window on Windows XP

I have a small GUI application that listens for network messages so a user can update some info and accept it. This is in a production factory environment and used for interacting with a specific piece of physical hardware (over serial in some cases). The workflow looks like this:
User is interacting with another program (5250 Green Screen)
They enter a certain keybinding that sends a UDP message to a Tkinter GUI
The Tkinter GUI does a deiconify()
User edits data, accepts (Enter) and it does an iconify()
My issue is that on windows XP, the GUI does not become active when I do the deiconify and conversely does not fall back to the prior window on iconify. I have tried some things I found in other questions such as:
Setting the Tk GUI as top.. self.wm_attributes("-topmost", 1)
Trying to set/force focus... self.focus_set() and self.focus_force()
Although the window is visible with the first, I can not seem to get it to be the active window so that the user can type in it without "clicking" on it to activate. The same is true for releasing the "focus" so that the active window becomes the one they were previously on (5250).
It seems like an issue that others also have had but I have not been able to find anything that works. Is there a programmatic way to get the window activated and release it when done?
Unfortunately, after a week there have been no answers and I was not able to find a direct way to do this with Tkinter. I did find a way to solve the problem though and it appears to work consistently. Here are the steps I took to make the screens activate:
Install pywin32.
Create a function that activates the tk app.
Create a function that activates the 5250.
Then each time I do a iconify/deiconify I also run the function to activate the appropriate screen. The code that activates the tk window looks like this:
def activate_self(self):
""" Activate this window. """
shell = win32com.client.Dispatch('WScript.Shell')
shell.AppActivate(str(self.title))
shell = None
The code that activates the caller is a little ugly since it has to guess the title but is the same basic concept.

windows system wide keyboard input emulation in python

I'm sending keyboard signals to the OS (Windows) via python. The problem is the specific application (Project 64, Nintendo 64 emulator) is not receiving the signals, I'm not sure why. The keys are registered in Word and my browser for example.
shell = win32com.client.Dispatch("WScript.Shell")
ser=serial.Serial('com10',115200)
while 1:
datain=ser.read(1)
if datain=='':
continue
datain_int=int(binascii.hexlify(datain), 16)
datain_bin=bin(datain_int)
if datain_int==0:
continue
print(datain_int)
if datain_int==128:
shell.SendKeys("a")
Since other applications are receiving your keystrokes it is entirely possible that the application, being an emulator, is scanning the keyboard directly rather than having keystrokes passed from the operating system.
If this is the case you would probably be better off downloading the source code and patching it to use input from the serial port directly. You an also have a look at the source code to see how it is receiving the keyboard events.

Simulating Key Press event using Python for Linux

I am writing a script to automate running a particular model. When the model fails, it waits for a user input (Enter key). I can detect when the model has failed, but I am not able to use python (on linux) to simulate a key press event. Windows has the SendKeys library to do this but I was wondering if there is a similar library for python on linux.
Thanks!
Have a look at this https://github.com/SavinaRoja/PyUserInput
its cross-platform control for mouse and keyboard in python
Keyboard control works on X11(linux) and Windows systems. But no mac support(when i wrote this answer).
from pykeyboard import PyKeyboard
k = PyKeyboard()
# To Create an Alt+Tab combo
k.press_key(k.alt_key)
k.tap_key(k.tab_key)
k.release_key(k.alt_key)
A more low-level approach would be to create an uinput device from which you would then inject input events into the linux input subsystem. Consider the following libraries:
python-uinput
evdev
Example of sending <enter> with the latter:
from evdev import uinput, ecodes as e
with uinput.UInput() as ui:
ui.write(e.EV_KEY, e.KEY_ENTER, 1)
ui.write(e.EV_KEY, e.KEY_ENTER, 0)
ui.syn()
If the "model" is running graphically (with the X window system), the already-suggested xsendkey is a possibility, or xsendkeycode. If it's running textually (in a terminal window), then pexpect.
I recommend PyAutoGui. It's ridiculously simple to use, it's cross-platform and it's for Python 3 and 2.
In the linked page are listed the dependences and some code examples.
http://people.csail.mit.edu/adonovan/hacks/xsendkey.html
As many of the solutions I have found in this and in another well ranked SO response were either deprecated (PyUserInput) or using evdev, which failed (UInputError: "/dev/uinput" cannot be opened for writing) the simplest solution for me using Linux was pynput. One example directly from their docs:
from pynput.keyboard import Key, Controller
keyboard = Controller()
# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)
# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')
# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
keyboard.press('a')
keyboard.release('a')
# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')
It worked like a charm!

Categories