pywinauto with a popup window - python

Im working through pywinauto, not a great developer but I can write some of the basics in python. Im getting hung up though. I have a popup that is causing me to not be able to press ok on and really not sure what direction I need to look
Really a two part question:
How can I move over to this popup IF it occurs, this wont always be the case as sometimes those files may not exist.
I tried using app.Confirm.Ok.click() and also app.Confirm.type('{ENTER}') neither worked.
How can I add in variables that I could call from an external .txt file for things like the "localhost" I included there in my code?
Code here:
from pywinauto import application
import time
app = application.Application()
app.start("Install.exe")
app.SelectLanguage.Ok.click()
app.Platform.Iacceptthetermsinthelicenseagreement.click()
app.Platform.Next.click()
app.Platform.Next.click()
app.PlatformInstallationOptions.Next.click()
app.PlatformSpecifyCertifcate.comboBox.select(0)
app.PlatformSpecifyCertifcate.Next.click()
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('localhost')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('{SPACE}')
app.PlatformConfigurationDatabaseOptions.type_keys('{TAB}')
app.PlatformConfigurationDatabaseOptions.type_keys('Config')
app.PlatformConfigurationDatabaseOptions.Next.click()
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{TAB}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{TAB}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{RIGHT}')
app.PlatformSpecifyHTTPSBindingCertifcate.type_keys('{SPACE}')
app.PlatformSpecifyHTTPSBindingCertifcate.Next.click()
app.PlatformAdvancedWorkflowSettings.Next.click()
app.PlatformPlatformLanguage.Next.click()
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('localhost')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('{SPACE}')
app.PlatformInstanceDatabaseOptions.type_keys('{TAB}')
app.PlatformInstanceDatabaseOptions.type_keys('Instance')
app.PlatformInstanceDatabaseOptions.Next.click()
app.PlatformTimeZone.Next.click()
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{TAB}')
app.WebApplicationOptions.type_keys('{UP}')
app.WebApplicationOptions.Next.click()
app.WebApplicationOptions.type_keys('{ENTER}')

confirmWin = .app.window(title_re = u'Confirm') #Check your window header object name.
# Use timeout based on average pop up time in your application.
if confirmWin.exists(timeout=10, retry_interval=1):
confirmWin.set_focus()
yesBtn = confirmWin[u'&Yes']
# Check the object name of the Yes button. You can use Swapy tool(It is deprecated but it works, else you can use inspect.exe)
yesBtn.click()
else:
print('Confirmation pop up did not appear')
This should work :)

Related

how can i take speech recognition in hear.py and put it in a entry box in tkinter chatbot_gui.py?

file name is hear, Lquery = r.recognize_google(audio).lower() is varible form the sr function listen.
self.msg_entry=Entry(bottom_label,bg="#2C3E50",fg=TEXT_COLOR,font=FONT)
self.msg_entry.focus()
self.msg_entry.bind("<Return>", self.on_enter)
#send button which will call on_enter function to send the query
send_button=Button(bottom_label,text="Send",font=FONT_BOLD,width=8,bg="Green",command=lambda: self.on_enter(None))
send_button.place(relx=0.80,rely=0.008,relheight=0.06,relwidth=0.20)
I tried adding: self.msg_entry.insert(0, env.hear.Lquery)
I tried adding a third file env. that imports both import hear.py and import chatbot_gui.py(trying to avoid the circular dependency (but it doesn't work)
I tried to write from hear.py (chatbot is the class) add_bot(function). cb().add_bot().msg.entry.insert(END,keyboard.write(Lquery))...which tells me add_bot is a None type and has no attribute msg_entry.
I tried a million other things.
I need to add hear.listen() to Chatbot_gui.py for sr
Just can't get anything to work. The gui should accept keyboard input as well as voice.

Python 2.7: Close a dialog without exiting the entire application

I'm running a script coded in python from a scripts menu in a desktop application. It's basically a giant macro that I wrote and added a GUI to. I'm pretty sure the GUI is a really old one that my desktop app uses called dialogKit from MIT.
GitHub still has it here.
The problem is the word "stop" at the very end of the dialog code.
I keep getting a "stop is undefined" message, which I understand, but I've tried everything to close the dialog and if I use exit(), sys.exit(), I don't get an error, but it also closes my entire desktop app.
I need to close the dialog and keep the software open.
The limited dialog documentation for what I'm using can be found here.
(you might have to click on the Dialog section. Their site uses frames.)
class MyDialog:
def __init__(self):
self.d = Dialog(self)
self.d.size = Point(300, 340)
self.d.Center()
self.d.title = "Halftone" #<----- Title of the dialogue
self.d.AddControl(STATICCONTROL, Rect(aIDENT, aIDENT, aIDENT, aIDENT), "frame", STYLE_FRAME)
# more controls and methods..
def on_ok(self, code):
return 1
def on_cancel(self, code):
print "blah"
def Run(self):
return self.d.Run()
d = MyDialog()
if d.Run()!= 1:
stop
I just need a way to change stop to something that 1) will prevent the script from running, and 2) close the dialog without quitting the entire application. This is the functionality of a typical "cancel" button, which is what I want.
Another option is the method called on_cancel(), which I also tried and could get the event itself to work, but still the entire application quits with any kind of exit().
The docs show a method called End(), which claims to terminate the dialog object, but I've tried and failed to get that to work either.
Okay, I'm posting an answer because I think I have a handle on your problem.
Try replacing stop with:
d.d.End()
If that works, you might want to try putting:
self.d.End()
inside of the on_cancel function in your class. That should close the dialogue without closing your program.

Python, Pyautogui, and CTRL-C

I am attempting to complete a simple process of opening a web/browser based document, selecting a field within said document, and then copying it so that it goes into my operating system's clipboard. Here's the specs :
Windows 7
Google Chrome ( latest stable )
Python 3.5
pyautogui for keyboard/mouse control
Here is the field I am trying to work with ( http://screencast.com/t/jt0kTagb ). When that little arrow is clicked it pops open to reveal a calendar to pick a date. If you click directly in the field instead it highlights the field's contents. When I manually press CTRL+C in this situation the field's contents go right into the clipboard as expected.
I've tried two methods of getting the field to go into my clipboard. The first was leveraging pyautogui's keyDown/up and press functions which essentially looked like :
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20)
pyautogui.keyDown('ctrl')
pyautogui.press('c')
pyautogui.keyUp('ctrl')
I then attempted to just use the app menu that appears if you right click on something which looked like this:
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20, button='right')
pyautogui.press("down", presses=2)
time.sleep(1)
pyautogui.press('enter')
Lastly I tried the pyautogui.hotkey() function which looked like this :
imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20, button='right')
pyautogui.hotKey('ctrl', 'c')
In all three events the field is indeed selected and as best as I can tell the keypresses are going through as all other presses/functions that happen prior go off without a hitch.
The problem that I am facing is that when I do this manually in the same fashion as both of those scripts above I am able to get the contents. When I use the scripts, the clipboard is never updated/populated with the field's contents. Is there something I am overlooking or not considering when working with Python and Window's clipboard?
In the end all I am trying to do is put that value into an excel sheet. Any advice would be appreciated!
I have also discovered this issue on a different automation script, and have been working on troubleshooting it for several days. I'm also on Python 3.5 and Windows 7. I can rule out that it has anything to do with Google Chrome, as my particular script is actually working with SAP.
The documentation for pyautogui on Read the Docs (https://pyautogui.readthedocs.io/en/latest/cheatsheet.html#keyboard-functions) gives a direct example of using Ctrl + C to copy text to the clipboard, so I can verify you're not actually doing something wrong. I believe you're just looking at a bug here.
I have opened an issue on the project's GitHub page:
https://github.com/asweigart/pyautogui/issues/102
Use the PyAutoGui module.
pip install PyAutoGUI
We can easily use HotKey combinations.
See docs: https://pyautogui.readthedocs.io/en/latest/keyboard.html#the-hotkey-function
Pressing Ctrl+C
>>> import pyautogui
>>> pyautogui.hotkey('ctrl', 'c')
I found the solution!
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
In my script I had to use root.update() after.

Some questions about switches in python

This is my first Question here at StackOverflow, so please be patient with me if some info isn't present or I missed something important, but anyways i'll do my best :)
Recently I started to code in Python2.7, so I'm not very good at it. While playing with PyGtk, PyGObject, Glade, etc I found something particular about switches (Haven't tried with any other widget, so I don't know if it happens somewhere else. Most likely it doesn't, I hope...)
I made a very basic GUI with a single "window" plus a "switch" using Glade
My objective was to deactivate switch after user tried to activate it if some exeption raised up before, something like:
Activate it --> * Found error --> * Deactivate it
I made some code, and after a while, I noted that THIS piece of code created a loop-like block, blocking GUI's window afterwards:
builder = Gtk.Builder()
window1 = builder.get_object('window')
switchie = builder.get_object('switchie')
switchie.set_active(False)
def Hi(switch, active):
print switchie.get_active()
switchie.set_active(not switchie.get_active())
switchie.connect("""notify::active""", Hi)
window1.set_position(Gtk.WindowPosition.CENTER)
window1.connect("delete-event", Gtk.main_quit)
window1.show_all()
If i'm right, "switchie.connect" links "switchie" object with "Hi" func whenever "switchie" gets clicked.
But if I execute this and try to turn switch on, GUI hangs up. I did try to execute this via script & command-line and adding the "print switch state", resulting in an endless loop (True & False)
I tried with many other funcs I made, but neither of them could solve this issue. In fact, this is the "essence" of all the other funcs I made.
Why does this happen?
Where's the loop?
Am I wrong in some line?
Help is appreciated!
(If you need to see the rest of my faulty funcs, just ask for 'em, but I don't think they'll help...)
You want to hook up the switch like this:
switchie.connect("""activate""", Hi)
This will only get called once for every time it is clicked. What you were doing is hooking up to the signal after it changed, so it was constantly changing, and never catching up. You will also want to change
def Hi(switch, active):
to
def Hi(switch, active = None):
for keyboard support.

How to stop a warning dialog from halting execution of a Python program that's controlling it?

Using Win32GUI and Watsup, I'm writing a bit of Python code to automate a search across a database that is accessed through a program that doesn't come with an interface for it. As such, I can take a string from a list and then input it into the search box and press 'lookup'.
However, when the search returns more than 1000 results, the program throws a warning dialog --which is simply a notification of the number of results--which halts the execution of the Python code. I can't get the code to progress past the line where it presses lookup.
At a guess, this would be because it doesn't expect a window or know how to handle a warning--but I don't either, other than manually accepting it. Below is the relevent sample of code, though it's probably not very enlightening. After "clickButton(LookupButton)", the execution halts.
LookupButtonlocation = elemstring.find("Lookup", AuthNameFieldlocation) - 15
#Use Regex search to find handles
number_regex = re.compile(';(\d+);')
AuthNameEdit = int(number_regex.search(elemstring[AuthNameFieldlocation:]).group(1))
LookupButton = int(number_regex.search(elemstring[LookupButtonlocation:]).group(1))
#Input new Author into Edit Field
setEditText(AuthNameEdit, "John Campbell")
#Click lookup button
clickButton(LookupButton)
I'm not a WATSUP user, but I do something very similar using pywinauto - in my case I'm running a number of automated tests that open various 3rd party programs that, in a similar way, throw up inconvenient warning dialogs. It's a bit difficult to deal with dialogs that you don't know about, however if you do know which dialogs appear, but not when they appear, you can start a thread to just deal with those pop-ups. The following is a simple example from what I'm doing, and uses pywinauto but you could adapt the approach for WATSUP:
import time
import threading
class ClearPopupThread(threading.Thread):
def __init__(self, window_name, button_name, quit_event):
threading.Thread.__init__(self)
self.quit_event = quit_event
self.window_name = window_name
self.button_name = button_name
def run(self):
from pywinauto import application, findwindows
while True:
try:
handles = findwindows.find_windows(title=self.window_name)
except findwindows.WindowNotFoundError:
pass #Just do nothing if the pop-up dialog was not found
else: #The window was found, so click the button
for hwnd in handles:
app = application.Application()
app.Connect(handle=hwnd)
popup = app[self.window_name]
button = getattr(popup, self.button_name)
button.Click()
if self.quit_event.is_set():
break
time.sleep(1) #should help reduce cpu load a little for this thread
Essentially this thread is just an infinite loop that looks for a pop-up window by name, and if it finds it, it clicks on a button to close the window. If you have many pop-up windows you can open one thread per popup (bug that's not overly efficient, though). Because it's an infinite loop, I have the thread looking to see if an event is set, to allow me to stop the thread from my main program. So, in the main program I do something like this:
#Start the thread
quit_event = threading.Event()
mythread = ClearPopupThread('Window Popup Title', 'Yes button', quit_event)
# ...
# My program does it's thing here
# ...
# When my program is done I need to end the thread
quit_event.set()
This is not necessarily the only way to deal with your issue, but is a way that's worked for me. Sorry I can't really help you much with dealing with WATSUP (I always found pywinauto a bit easier to use), but I noticed on the WATSUP homepage (http://www.tizmoi.net/watsup/intro.html), Example 2 does something similar without using threads, i.e., looks for a named window and clicks a specific button on that window.

Categories

Resources