Python Selenuim Alert Box Disable? - python

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.

Related

How can I click the close button in Gmail with Python + Selenium?

I want to click on the button to close the message that is being written.
I try with this code, but it does not work for me:
driver.find_element_by_xpath('//input[#type="Cerrar"]').click()
Can you help me? Thanks very much.
You need to click this element: //img[#alt="Close"]
So, the Selenium python command to do this is
driver.find_element_by_xpath('//img[#alt="Close"]').click()
unless your code needs to use selenium i think that pyautogui would be better for this. it can move your mouse to a specific coordinate or look for the close button and then click it
import pyautogui
pyautogui.moveTo(x=(your x), y=(your y))
pyautogui.click()
i dont know much about selenium so im sorry i cant use that to help you but i hope this helps.
The built-in method close:
The close() method is used to close the current browser window on which the focus is set, on the other hand, the quit() method essentially calls the driver. dispose of a method that successively closes all the browser windows and ends the WebDriver session graciously.
You want to use click when interacting with elements
driver.find_element_by_xpath('//input[#type="Cerrar"]').click()
If that doesn't work, please share the HTML code.

Struggle clicking a a window closing button with selenium

I'm struggling to click on a specific object with selenium. HTML CODE
What I've tried so far, unsuccessfully:
webD.find_element_by_link_text('Apply').click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div/div[4]/div/div[3]/button'))).click()
webD.find_element_by_xpath('/html/body/div[3]/div/div/div[4]/div/div[3]/button').click()
WebDriverWait(webD, 20).until(EC.presence_of_element_located((By.LINK_TEXT, 'Apply'))).click()
And myriads in between. The final code is the one I was most optimistic about.... Turns out the button changes from "close" to "apply" once a selection is done, so I figured by waiting for the element to appear and then click it could work. I think I'm missing something here. Maybe I don't require a wait at all..
Clicking the apply button closes the selection window (which is what I'm trying to do).
Any insight on why these are wrong? Any idea how to correct this issue? Thank you so much in advance! Looking forward to reading your answers.
Use following xpath to click on the button.
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Apply')]"))).click()

Python - Selenium can't handle alert

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

**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