click() does perform a click but ends with an error - python

I have a button on a dialog box which when it is clicked, pops up a confirm popup window.
Whenever I perform a click on that button using pywinauto, the click does occur in the UI, however the click() function does not return without throwing this error:
COMError: (-2147220991, 'An event was unable to invoke any of the subscribers', (None, None, None, 0, None))
The code to click the button is very simple:
readerDlg = mainDlg.window(title=READER_WINDOW_TITLE)
readerDlg.Skip.click()
Connection snippet:
sw = Application(backend='uia').connect(title=APPLICATION_TITLE)
mainDlg = sw.window(title=MAIN_WINDOW_TITLE)

Switching the backend from uia to win32 fixed my issue:
sw = Application(backend='win32').connect(title=APPLICATION_TITLE)
It also made the automation much faster (from ~5 sec/op to < 1 sec/op.

Related

pyqt6 QMessageBox blocking unittest

In a window class I have this button...
encrypt_button = QPushButton("Generate")
encrypt_button.setProperty('class', 'warning')
encrypt_button.clicked.connect(
lambda: encrypt_message(encrypt_message_box.toPlainText(), encrypt_public_key_box.toPlainText(),
encrypt_output_box))
encrypt_button.setObjectName("encrypt_button")
The encrypt_message function opens a error QMessageBox if there are values missing...and all works fine.
But I've come to write a unittest and I cannot seem to test for it opening...when I simulate the button click the window actually opens and will not continue until I click OK
def test_copy(self):
""" checks to make sure the button doesn't copy when its blank """
for button in self.window.copy_buttons:
button.click()
##some test here
I've enlisted the help of our friendly AI helper, who keeps telling me to use QSignalSpy or qWaitForWindowExposed, but everything I try just results in the same thing. The QMessageBox opens and I'm stuck waiting for user input.
Any suggestions?

Handling Context Menu of the taskbar icon with pywinauto

I am trying to automate the exiting operation on one of the apps. The app's icon is located in the taskbar. I was successfull in opening that icon's context menu with the modified code that I have found on stackoverflow:
import pywinauto
from pywinauto.application import Application
import time
app= "Service is enabled."
app = Application(backend="uia").connect(path="explorer.exe")
st = app.window(class_name="Shell_TrayWnd")
t = st.child_window(title="Notification Chevron").wrapper_object()
t.click()
time.sleep(1)
list_box = Application(backend="uia").connect(class_name="NotifyIconOverflowWindow")
list_box_win = list_box.window(class_name="NotifyIconOverflowWindow")
list_box_win.wait('visible', timeout=30, retry_interval=3)
# time.sleep(1)
appOpened= list_box_win.child_window(title = app)
appOpened.click_input(button = "right")
After the execution of the code above I get to the point when the context menu is opened:
The next thing that I want to do is to click on Exit, I have tried doing it by specifying the mouse click coordinates, but I have noticed that the position of the parent icon is changing from time to time.
What I would like to do is to get the handle on the Exit button and send click automatically.
------Edit-------
The icon is located in the hidden icons
So you want to access to the right click context menu. As said in this answer, you can do something like :
listbox.PopupMenu["Exit"].set_focus().click_input()

GUI Automation of policy change for logoff button. pywinauto.findwindows.ElementNotFoundError: error. How to switch context?

I am automating the steps to change the policy for the logoff button. The steps involved are:
Open Local Group Policy using gpedit.msc
Select "Start Menu and Taskbar" from the dropdown in User Configuration > Administrative Template from the left pane
In the right pane, double click on "Change Start Menu power button"
Select the radio button "Enabled"
From the dropdown menu of options: Select "Log Off"
I have gotten through the third step, but I have a problem in mapping the "Change Start Menu Power Button" from the second step. My code is as below:
from pywinauto import Application
Application().start(r'mmc gpedit.msc')
app = Application(backend="uia").connect(path='mmc.exe')
#app.LocalGroupPolicyEditor.dump_tree()
Admin_template = app.LocalGroupPolicyEditor.child_window(title="User
Configuration", control_type="TreeItem").child_window(title="Administrative
Templates", control_type="TreeItem") # since there are same templates
Admin_template.double_click_input() # it expands the subtree
#Admin_template.dump_tree()
Start_menu = Admin_template.child_window(title="Start Menu and Taskbar",
control_type="TreeItem").double_click_input()
Start_menu.dump_tree()
#Admin_template.child_window(title="Start Menu and Taskbar",
control_type="TreeItem").dump_tree()
#Change_start_menu = Start_menu.child_window(title="Change Start Menu power
#button", control_type="MenuItem").double_click_input()
#Change_start_menu.dump_tree()
I have trouble in finding and mapping the elements in the right pane. Also, when I use Start_menu.dump_tree(), there are only "Notification" elements shown. However, the rest, which includes "Change Start Menu power button," is what I'll be double clicking next.
I appreciate the help. Thanks.
It was kinda tricky, but this should do the job (it does all the steps you've listed - presses OK - and closes the program):
import pywinauto
pywinauto.Application().start(r'mmc gpedit.msc')
app = pywinauto.Application(backend="uia").connect(path='mmc.exe')
admin_template = app.LocalGroupPolicyEditor.child_window(title="User Configuration", control_type="TreeItem").child_window(title="Administrative Templates", control_type="TreeItem")
admin_template.double_click_input()
start_menu = admin_template.child_window(title="Start Menu and Taskbar", control_type="TreeItem")
start_menu.double_click_input()
option_list = app.LocalGroupPolicyEditor.child_window(auto_id="12786", control_type="List")
# Just select any of the first options to change the focus to the list.
first_elem = option_list.child_window(title="Add Search Internet link to Start Menu", control_type="ListItem")
first_elem.click_input()
# Used to scroll down the window so that the wanted option becomes visible.
pywinauto.keyboard.send_keys("cccc")
option = option_list.child_window(title="Change Start Menu power button", control_type="ListItem")
option.double_click_input()
pop_up = app.LocalGroupPolicyEditor.child_window(auto_id="tableLayoutFullForm", control_type="Pane")
radio = pop_up.child_window(title="Enabled", auto_id="radioButtonEnabled", control_type="RadioButton")
radio.click_input()
drop_down = pop_up.child_window(title="Choose one of the following actions", auto_id="dropDownListChoose one of the following actions", control_type="ComboBox")
drop_down.click_input()
# 'Hack' to first select the Restart option and then the next option after that which starts with l (=Log off).
# This ensures that the correct setting gets set despite of what the setting was before.
pywinauto.keyboard.send_keys("rl{ENTER}")
ok = pop_up.child_window(title="OK", auto_id="buttonOK", control_type="Button")
ok.click_input()
app.kill()
Make sure to run this script as administrator or else it will fail.
Feel free to ask if you got any questions about the code :)
Edit:
If you're running a version of pywinauto <0.6.0, you'll have to replace the two occurances (lines 19 and 34) of pywinauto.keyboard.send_keys() with:
pywinauto.SendKeysCtypes.SendKeys()
If that doesn't work you could try:
pywinauto.keyboard.SendKeys()

How to click button in 'alert' message on the webpage with PyQt4

On the webpage, when I create a new user, alert message displayed that 'New user was created'. And in order to continue, I have to click button 'ok'. So the thing is I don't really know how to click it.
when I need to click a regular button I do something like this:
doc = self.page().currentFrame().documentElement()
submit_button = doc.findFirst('input[id=my-submit-button]')
submit_button.evaluateJavaScript('this.click()')
But how to click button in alert message?
You are looking for the QWebPage::javaScriptAlert ( QWebFrame * frame, const QString & msg ) function:
This function is called whenever a JavaScript program running inside
frame calls the alert() function with the message msg.
The default implementation shows the message, msg, with
QMessageBox::information.

Windows System Menu in Python

I am trying to create a new item in the Windows System Menu for my app and have an action performed when it is click.
I have worked out the frist part using the below code.
import win32con
import win32gui
hwnd = win32gui.GetForegroundWindow ()
hmenu = win32gui.GetSystemMenu (hwnd, False)
win32gui.AppendMenu(hmenu, win32con.MF_SEPARATOR, 0, '')
win32gui.AppendMenu(hmenu, win32con.MF_STRING, 100, 'New Menu Item')
I can not work out how link a function to the button? So that an action is performed when it is click
Any help would be great.
Thanks.
Donald.
p.s. Using Python 2.6.5 and Windows XP
You have to hook into the windows procedure of the menu and handle the messages being sent. See e.g. http://wiki.wxpython.org/HookingTheWndProc

Categories

Resources