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

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.

Related

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

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.)

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?

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?

Python3, Selenium, Chromedriver console window

I've a made a selenium test using python3 and selenium library.
I've also used Tkinter to make a GUI to put some input on (account, password..).
I've managed to hide the console window for python by saving to the .pyw extension; and when I make an executable with my code, the console doesn't show up even if it's saved with .py extension.
However, everytime the chromedriver starts, it also starts a console window, and when the driver exists, this window does not.
so in a loop, i'm left with many webdriver consoles.
Is there a work around this to prevent the driver from launching a console everytime it runs ?
I hated dealing with this in selenium until I remembered that this was an obvious use case for context managers just like the usage of open.
I did find out that selenium is about to add this officially to their package in this pull request
Until this is officially added, this snippet should give you the functionality you need to get things going :)
import contextlib
#contextlib.contextmanager
def Chrome(*args, **kwargs):
webdriver = webdriver.Chrome(*args, **kwargs)
try:
yield webdriver
finally:
webdriver.quit()
with Chrome() as driver:
# whatever you're planning on doing goes here
driver.close() and driver.quit() are two different methods for closing the browser session in Selenium WebDriver.
driver.close() - It closes the the browser window on which the focus is set.
driver.quit() – It basically calls driver.dispose method which in turn closes all the browser windows and ends the WebDriver session gracefully.
You should use driver.quit whenever you want to end the program. It will close all opened browser window and terminates the WebDriver session. If you do not use driver.quit at the end of program, WebDriver session will not close properly and files would not be cleared off memory. This may result in memory leak errors.

Python Selenium: How to let human interaction in a automated script?

I am working on a script that shows CAPTCHA and a few other stuff in a pop window. I am writing script for FireFox. Is it possible that I feed the values and on hitting Submit button script could resume the operations? I guess, some kind of infinite loop?
You could wait for the submit button to be clicked by the user:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# load the page
driver.get("https://www.google.com/recaptcha/api2/demo")
# get the submit button
bt_submit = driver.find_element_by_css_selector("[type=submit]")
# wait for the user to click the submit button (check every 1s with a 1000s timeout)
WebDriverWait(driver, timeout=1000, poll_frequency=1) \
.until(EC.staleness_of(bt_submit))
print "submitted"
I believe what you're asking is if it's possible to have your selenium script pause for human interaction that can't be fully automated.
There are several ways to do this:
Easy but hacky feeling:
In your python script, put
import pdb
pdb.set_trace()
At the point you want to pause for the human. This will cause the python app to drop to a debugger. When the human has done their thing, they can type c and hit enter to continue running the selenium script.
Slightly less hacky feeling (and easier for the human).
At the point where you want to put the pause, assuming the user submits something, you can do something like (with an import time at the top):
for _ in xrange(100): # or loop forever, but this will allow it to timeout if the user falls asleep or whatever
if driver.get_current_url.find("captcha") == -1:
break
time.sleep(6) # wait 6 seconds which means the user has 10 minutes before timeout occurs
More elegant approaches are left as an exercise for the reader (I know there's a way you should be able to not have to busy-wait, but I haven't used it in too long)
One way is to check for some content of the next page that loads after entering captcha and wait till they're found.
Other way is to check for current URL until it changes (usually URL changes after entering CAPTCHA) or wait for the next URL with -
while driver.current_url == your_current_url:
wait

Categories