I'm trying to get PyWinAuto to click a button as soon as it's enabled. Here's what I currently have:
while running:
try:
app = pywinauto.Application().connect(title='Microsoft Outlook', class_name="#32770")['Microsoft Outlook']
app.Allow.Wait('ready', retry_interval=0.1)
app.Allow.Click()
print('Clicked')
except (pywinauto.findbestmatch.MatchError, pywinauto.findwindows.ElementNotFoundError):
time.sleep(0.1)
pass
This works just fine if I start it running after the button is active, clicking and printing 'Clicked' as expected. If I run it before the button is active, it waits for it as expected and then seems to try and click it - Printing 'clicked' repeatedly until I click either mouse button or press enter. If I take the click() out and get it to just return app.Allow then the result is as expected regardless of when I load the script, so it does seem to be click() that's the hangup.
The behaviour is the same regardless of where I click or which window I have active - It'll work if I click anywhere or anything, but it won't do anything at all until I do... Which defeats the object of the automation, really!
Any ideas?
Thanks!
First you have to run the script as Administrator if you use .connect(...). I've already added warning about that and error in the .click() method when target process has higher privileges. See pull request #499. It will be included into coming pywinauto==0.6.5.
There is one more method: .click_input() moves mouse cursor and performs real click. While .click() just sends WM_CLICK window message (might be useful for minimized or non-active window).
P.S. By the way, for Outlook I'd recommend using Application(backend="uia") and you'll have no Win32 API specific problems. See Getting Started Guide about backends difference.
Related
This has been asked before here: How to hide context click? Selenium Chromedriver
But none of the solutions there have worked for me. There is an element on my webpage that requires a right-click to select it (weird, but it makes sense with the site). I am able to click this element using
action.context_click(src).perform()
And I have tried to get rid of the menu that pops up after right-clicking 4 separate ways
# Left-clicking in the same spot after I have right-clicked
action.click().perform()
# Hitting the space bar
webdriver.ActionChains(self.driver).send_keys(Keys.SPACE).perform()
# Hitting the escape key
webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()
# Clicking on an unused element
action.move_to_element(WebDriverWait (self.driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "html"))))
action.click().perform()
# Reloading the page did not get rid of it either
self.driver.refresh()
None of these methods have worked to get rid of the menu that pops up. I cannot reload the page to get rid of this otherwise it will deselect the object I am trying to get. This does not affect my program but it is annoying to have it in my way when I am trying to spectate what my program is doing. Any help is appreciated! This is something in my free time so it is not time-sensitive.
I tried to open the context menu (right click) on a random site and then I was able to close it with pyautogui (pip install pyautogui)
action.context_click(src).perform()
import pyautogui
pyautogui.press('esc')
It may be possible that you have to press esc a couple of times.
I'm using Python and Selenium to write an automation script in Internet Explorer.
When the web page throws up some kind of modal dialog box, the Python code stops running and just waits for some action to be taken on the popup. After you press the "yes" or "no" button, then the Python code continues.
I believe the underlying Javascript function that is getting called (saveClicked()) is generating the popup box using this line of code:
var result=window.showModalDialog('whatever....')
Does anyone know how to handle this in Selenium? I want my code to click "ok" in this window or to just accept it. I tried right-clicking on the window to look at source code, etc. but those options are not given to me...the only options are "move/close".
I've looked to see if there is some kind of default IE capability in Selenium that will just automatically accept all modal dialog boxes but haven't found any. I also thought of maybe wrapping the call to the Javascript function with something that would somehow send a keystroke to the alert. I'm open to anything!
Here is the code: It never moves past the .execute_script line...it just sits there waiting.
print('Saving')
# I have to do this because I can't get the handle to the save button
# using any of the known Selenium methods but calling the JS works
driver.execute_script('saveClicked();')
print('Test')
driver.switch_to().alert().send_keys(Keys.ENTER)
The code just STOPS after the Javascript is executed and never moves to the print('test') line or any other code I put there.
Any python selenium code suggestions to solve this would be greatly appreciated.
one way: you can try to use Alert to manage popups
Alert(driver).accept()
otherwise you can see the active window or tab with:
#get current window handle
p = driver.current_window_handle
#get windows
chwd = driver.window_handles
driver.switch_to.window(chwd[1])
reference:
https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.alert
Try:
message = "saveClicked()" # or any other of your messages
driver.execute_script(f"alert(\"{message}\");")
driver.switch_to.alert.accept()
The webpage I need to go to has a javascript popup message with an OK button that appears before the page can finish loading.
There's ways to get rid of the message by either clicking the button, pressing enter, pressing the x to close, or pressing ALT+F4. But all attempts to either click or press keys fails and just remains on the page with the popup.
I must be missing something.
Everything is current and installed, even the registry additions for the IE server executable.
Thanks
If this Java popup is generated by IE you could be able to handle it using Alert(driver).accept()
I have run into this issue a couple of times where the popup is not part of IE at all and this does not work. there are a couple different methods you can try to get around this:
try switching your browser to phantomJS (this is a headless browser that works with selenium and runs in the background so you will not be able to see what selenium does anymore but it often will eliminate popups and is good if you don't actually need to click the popup)
try using the mouse or keyboard packages to hit that button.
Facing this issue with selenium ide with Python.
if driver.find_element_by_xpath('//*[#id="xwt_widget_form_TextButtonGroup_2"]/span[2]/span'):
driver.find_element_by_xpath('//*[#id="xwt_widget_form_TextButtonGroup_2"]/span[2]/span').click()
print "Clicked"
The Problem is when i tried pressing ok button in the popped up window as attached by using [![find_element_by _xpath][1]][1], then sometimes on running the script it is pressing the ok button and sometimes it is not.
I had already tried alert box handling and window handler for the multiple window. I had also tried different options for the different xpaths generated.
I am looking forward to an alternate solution to the xpath or a particular xpath value which can work everytime when I run the script.
This is my first encounter with selenium, so please excuse if this looks like basicquestion.
I am trying to automate some basic web browser manipulation. As part of this sequence there is a popup window that must be dealt with. Once the python code the causes the popup window to appear happens, no other code past that point will run until I manually close that window. Then all remaining code runs fine.
I've try this below to try to deal with handling the popup but to no avail:
for handle in browser.window_handles:
print "Handle = ",handle
browswer.switch_to_window(handle);
elem=browser.find_element_by_tag_name("title")
print elem.get_attrbitue("value")
I cannot even get a basic 'print' command to execute until I close the popup window. Is there a need to set some type of event handler BEFORE the code is called that brings the popup menu?
Thanks for any suggestions.