Python Selenium -- how to dismiss alert in endlessly loading window? - python

I'm trying to dismiss an alert, close a window, or suppress an alert or window from opening in the first place in Selenium (with Python), and I'm stuck with an infinitely loading window.
Screenshot of window loading
I'm working with the Selenium webdriver in Python, and when I enter text into one window's text box, in certain circumstances a new window automatically pops up with an alert in it. However, the window apparently loads infinitely. I do not need to interact with the window; I'd be fine if I could just close it out immediately.
But since the page is still loading, I cannot find a way to dismiss the alert or to force close the window. It just loads forever; the only thing I've been able to do is actually click the "OK" button myself, and this needs to work without manual user input.
Anyone know how I can either dismiss this alert or force the window to close? Or maybe suppress the window from opening or suppress the alert?
Thanks!
I've tried changing the page load timeout to then close the window or dismiss the alert, but that doesn't work. I cannot switch to the window and interact with it, and I cannot use driver.switch_to.alert.dismiss() or driver.close(); my code just hangs forever. I found that if I manually minimize the window, the alert is dismissed and I could probably interact with the window then, but I can't minimize the window with code; my code won't progress while the window is loading.
(Unfortunately this cannot be reproduced without someone using a private account to log into the system.)

Related

Interact with website without sending keystrokes to window (Python, Windows)?

Is it possible to interact with a webpage loaded into a web browser (such as Chrome) without the window being active and without sending keystrokes to it? For example, suppose I have SoundCloud loaded in chrome and the chrome window minimized, but I want to create a hotkey on my computer (such as through Autohotkey) which acts as a play/pause button for the track. Would it be possible to have a Python script somehow interact with the browser to obtain that functionality without having to send it a keystroke?
The reason I'm trying to avoid having to send keystrokes is because it would require the Window to become briefly maximized and active. I can already do this in autohotkey. For example, I have an ahk script that iterates over all the windows, finds one with Soundcloud in the title, maximizes the window if it is minimized, sends the spacebar keystroke (which acts as play/pause on Soundcloud), and then minimizes the Window again if it was minimized to begin with.
This has the undesirable effect of making the Window flash briefly if it was minimized, or if virtual desktops are used, all the Windows flash if the Chrome window with Soundcloud is located on another virtual desktop other than the active one.
Ideally I could just write some program that runs silently in the background to send some kind of the request to the site that has the same effect as pressing the play/pause button without having to use the janky keystroke method I suggested above. But I am not sure if this is possible. What is actually happening when I click the play/pause button on Soundcloud, and is there some way write a program to get Chrome to do that without using keystrokes?
Any suggestions? I would prefer to do this without any browser plugins if possible.

Python Selenium will stall if window is left in background

I have a setup where I create a Remote driver (with Chromedriver), go to a website, perform actions on it. I'm running the driver with headless=False, so Selenium creates a Chrome window.
When I have the Chrome window open and in foreground, everything works just fine.
When I have it reduced to icon, or even open but in background - so using other apps when Selenium is working -, then the driver can stall - but it will, like 80% of the times.
When this happens, I have to reopen the Chrome window and, if this fails to resume the execution, I have to go to the running script's terminal and press Enter - so send an event to the script -; usually this solves the stall, and the execution will continue.
I know that, when running with headless=True, this problem doesn't show up; however I cannot use it, since the website in question will change the page according to this setting, so my currently situation is running this script with this Chrome window in front of me.
The script execution will last from 5 to 10 minutes everytime it's runned, if this can may be related to the problem.
Is there anything I can do to mitigate, or even remove this problem at all?

Selenium Python getting around "Open <...>.app?"

I am trying to write e2e tests for a Slack bot and while logging in via browser it always asks whether I'd like to use the Slack desktop app instead of continuing with the browser (its Chrome by the way). Steps the Selenium webdriver is performing:
Visit https://company-name.slack.com
Fill in the email and password
Click Sign In button
Then this shows up:
This is not a normal browser alert but I'd like to get rid of it. I have tried the following:
webdriver.switch_to.alert.dismiss() Does not dismiss this pop-up
Adding the chrome_options.add_argument('--disable-default-apps') switch also doesn't prevent the pop-up from showing
Have tried the headless version as well, test failure suggests that the pop-up interrupted the flow.
This also has to work on CI servers so please if the solution didn't involve modifying developer machine then that would be wonderful.
Well, this is not an answer but a workaround. I managed to get around the default-app pop-up by continuing my operations in a new tab (But not before checking whether there is a pop-up in the first place). Psuedo code:
try
find_element_by_whatever(element_you_expect_after_login)
catch TimeoutException
webdriver.execute_script('window.open()')
webdriver.switch_to.window(webdriver.window_handles[1])
webdriver.get(url_which_required_login_in_the_first_place)
today I solved this problem, you can just simply install pynput module, after that :
from pynput.keyboard import Key, Controller
keyboard = Controller()
keyboard.press(Key.enter)
keyboard.release(Key.enter)
you can handle the open app popup in selenium with your keyboard.
try this chrome_options.add_argument("--disable-notifications") or chrome_options.add_argument("--disable-popup-window") or create a code for click the cancel button to do the next operation.
If this alert pops up every time you run the test, and the Cancel button is always in focus, then you can try to send the driver an ENTER.

Python Selenium Click to Open Chrome Print Dialog Hangs

I have a Chrome window with a print button that I am navigating with Selenium. I click the button like this:
driver.find_element_by_id('print_button_id').click()
The click is successful, because the Chrome print dialog box opens:
However, no code after the click runs, and it appears to hang until I manually click Cancel on the print dialog. I tried using other methods of clicking (e.g. send_keys(Keys.ENTER)), but the same result occurs.
The same thing occurs when I use JavaScript to execute the print command:
driver.execute_script("window.print()")
That line of code hangs until I manually interact with the print dialog. This prevents me from automatically clicking the Print button on the print dialog with ActionChains.
Why is the click hanging, despite successfully opening the Chrome print dialog?
Selenium interacts with browser and this dialog box is not a part of browser because of which nothing is happening in here.

Faster way of checking if dialog has closed?

I'm automating an application (pywinauto ver 0.6.2) in which I:
search for a record
open a dialog
make changes
click an OK button
wait for the dialog to close
when closed, search for the next record, repeat
In the 'wait for the dialog to close' step I am using:
dlg.wait_not("visible", timeout=60)
This works most of the time, but sometimes it not only doesn't detect that the dialog has closed, but it also doesn't appear to do anything at the 60 second mark.
Is there a better way to detect the close of a window?
The bug with timings is fixed in pywinauto 0.6.3. Please update it with pip install -U pywinauto. Method wait_not is also affected.

Categories