Python - Selenium can't handle alert - python

I'm trying to automate a procedure on a really old IE-only webpage and, at one point, it raises an alert.
I can't inspect it using IE (or don't know how) but there's only an "accept" button (actually an image) and the alert text can't be copied (not sure why).
I'm using selenium with the IE drivers and i can't get past this alert.
Selenium IS detecting the alert, but when i check its contents i get nothing.
I've tried accepting the alert with
alert_obj = self.br.switch_to.alert
alert_obj.accept()
and also
.dismiss();
.send_key(Keys.ENTER)
and some other things. Am i missing something?
photo of alert:

alerts are generic dialog boxes that include a text and ok button. If your alert has an image as button, then it is not an alert, but something else. I don't know what it is, but not an alert.
This is an alert:

If this popup is an alert, you can handle it while initializing driver itself. Below is C# Code for that:
InternetExplorerOptions options = new InternetExplorerOptions
{
UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,
};
driver = new InternetExplorerDriver(options);
Above code should handle alert. if not, try adding this code before creating driver instance.
options.AddAdditionalCapability("browserstack.ie.enablePopups", "accept");

This question is a possible duplicate of:
How to close yet another chrome popup using python selenium
To moderators: I tried to flag this question as Duplicate, but then, when I typed "How to close yet another chrome popup using python selenium" into the search bar it found no results.

I somehow managed to do it.
I tried everything and everything was buggy. I even had to use this weird loop because just using a time.sleep() call was bugging things up. I really don't know what happened. Sometimes when entering the page the pop-up handle would appear and sometimes not. Sometimes in the wrong place. Sometimes it wouldn't close properly. I tried a pile of different ways and this one seems to work:
Here's what i did:
#Saves ID from original window
janelaOriginal = self.br.current_window_handle
#Go to the website
self.br.get(url)
#waits 2.5 seconds for the pop-up (time.sleep bugs)
i = 0
while(i < 25):
i += 1
time.sleep(0.1)
#is pop-up open?:
if(len(self.br.window_handles)>1):
#handle sometimes appears in the wrong place so this is necessary:
if(janelaOriginal==self.br.window_handles[0]):
self.br.switch_to_window(self.br.window_handles[1])
else:
self.br.switch_to_window(self.br.window_handles[0])
#close the pop-up and go back to the original window
self.br.close()
self.br.switch_to_window(janelaOriginal)
#do stuff
return
#do other stuff

Related

Python Selenuim Alert Box Disable?

How can I tell selenium to press cancel in this case or remove the whole box completely. I looked around and only really saw C# answers. Thank you for your time :)
At first glance, it seems like that's an Alert.
In Python-Selenium you could do this:
Alert = driver.switch_to.alert
Alert.dismiss()
However, It could be a pop-up in HTMLDOM as well. In this case, you will have to find the right locator (CSS, XPath etc.) and perform a click on that cancel button.

Python3.x, How to focus on newly opened webpage

Ok, here's the deal. I use:
driver = webdriver.Firefox(firefox_profile=profile)
driver.get = ('http://google.com')
to open a new webpage. After opening the new webpage, I use pyautogui to do some clicking and scrolling and whatnot, and then close the browser, and then do the same thing over again as the program iterates through a list of proxies. But when the webpage opens, it isn't in focus. I can see it and see that it opened, but it's not in focus.
So I originally implemented a simple click command on the webpage, and that usually focuses the new browser. But after the first browser is opened and closed, the next browser that is opened using a new proxy from the list isn't in focus, and for whatever reason, the click doesn't bring it into focus. So because the browser isn't in focus, my scroll code doesn't happen, it just acts like it skips over it. So I guess my question is, is there some command I can use to make my program focus on the new browser? I've searched extensively on here for the answer to my question and have tried a bunch of different things, but none have worked. Most answers on here pertain to newly opened tabs, and that isn't my issue. Any helpful comments or ideas would be much appreciated. I'm using Mac OS X, and the most recent version of python. Thank you.
Scratch the whole click on the dock icon thing, after extensive testing, that didn't work consistently. However, what did actually work consistently was using pyautogui to click on the "new tab" button at the top, and then close tab, and boom, webpage focused.

windows warning starting chrome with Selenium Python

I've just started coding in python with selenium, but here is my problem :
When I start Chrome with selenium,I ve a warning message from windows (my computer is in french, but it ask if I want to reset my setting). And I would like to close that tab, to make my program continue on another tab.
But it seems impossible to close it. I tried :
the Alert class of selenium, did nothing, the code just go through it
Moving mouse to the cross, or to "dismiss" (with selenium obviously) just the same
Switching tab, and close it (with selenium's control command), it doesnt change anything
Open Chrome with my usual settings (still from selenium), with that mode i can t open my url anymore, but i dont have the warning message too ...
I would be so grateful if someone had an idea, or have already had that problem. In fact, i would just want to dismiss it, to make my program run, without having to close the window at the start.
Apologize me for my english, that's not my native language, but I swear that I do my best :/
Thank you very much to all, feel free to ask me more informations :)
AR
Edit: I can't forbid you to put a -1, but try to explain me in which way it deserves it, then, I'll try to correct me, or to give more details.
Here the solution, even if I would prefer a patch from Selenium, because to my mind, that is a trouble comming from Selenium/python + windows + chrome combination : I ve created a new profile on chrome, add the X.add_argument("user-data-dir=...) It doesn't open my profile (I've maybe done a mistake in the path to the folder) BUT the warning message disappears with that line, all I wanted, then thank you very much Bill Bell ;)

**Python Selenium Behave** Alert box automatically closes after going to next step

I am in a project in which I am making a modular behavior driven framework for the company I am working right now. In making a modular approach of the step "user accepts alert", when I test it and came an expected alert box, it automatically closes itself and therefore shows this "NoAlertPresentException: Message: No alert is present" exception.
I have done this codes so far:
def acceptalert():
alert = driver.switch_to.alert
alert.accept()
driver.switch_to.parent_frame()
This code snippet works as I have those modules in which I incorporated the closing of alert box. The only problem is just when I try to make this one a standalone module in my framework. I have done research with this one but I really never got my problem answered. I hope there will be one in here who can help me. Thank you very much.
Found the answer. Thank you. I implemented the step "user clicks '' button" where is the value of the button being matched with a series of xpath and if the xpath returns zero match, then it fetches the available iframes and loops inside each one. When the xpath still returned zero matches, then it switches back to the main frame with this peace of code:
driver.switch_to.parent_frame()
The side effect with this one is it will dismiss the alert boxes. My problem is solved. Thanks everyone.

Selenium PhantomJS throws EelementNotVisible while Firefox is completely fine during combo box selection

So I have a website which has a combo that I need to select an item from, now the problem is it's a bit untraditional and doesn't have option's as elements but instead it has divs.
So I need my program to click combo box then wait (the best way to do this I found is via implicitly_wait(3)# 3 seconds) and then click the box element I need. Firefox is doing a great job with it but PhantomJS seem to throw:
Selenium.common.exceptions.ElementNotVisibleException:
Message: 'Error Message => \'Element is not currently visible and may not be manipulated\'
I'm not sure what's the cause of it, but I suspect that PhantomJS fails to correctly wait via implicitly_wait for some reason and tries to select non-visible element.
Any idea how to approach this without forced thread sleep?
Yup, your issue sounds exactly something I've just fixed in a UI test that was starting to anoy me. Quite a complex one, that passed on all browsers, except my favorite PhantomJs (which is the fastest).
It was quite anoying, when in the debugger I could clearly see that even the parent element was set to visible. Only the prime faces component I needed to click for whatever reason (not CSS or active Styles) was not visible.
After looking at Phantom JS screenshot, I realized the window was quite tiny, and indeed the UI element was not visually visible.
You see now where I am geting at?
Phantom JS is a headless browser, it does not render you the window, but it does use a window, and the window does have a size.
The exception is quite unexpected, because the engine behind is the same as the one used for chrome and safari ... things should just work.
Well, and they did in the end.
Try to tune your driver factory like this:
WebDriver driver = new PhantomJSDriver();
driver.manage().window().setSize(new Dimension(800, 600));
return driver;
Good luck.

Categories