Riaz Ladhani
Riaz Ladhani

Reputation: 4062

Python Webdriver how to handle a browser pop up. It is not an alert generated by JavaScript

I have come accross a pop up while my Python Webdriver script was executing. Scenario: User can click Add project button from the Admin page and enter a project name etc click Save. If the project already exists a pop up is shown to say project with this name already exists.

I want to close this pop up. I have inspected the HTML and there is no on click JavaScript function to call the alert. It is not a JavaScript alert. It is a pop up generated by GWT in the browser.
The dev uses GWT to develop the UI.

The HTML is:

<div class="gwt-PopupPanel" style="visibility: visible; overflow: visible; position: absolute; left: 687px; top: 170px;">
    <div class="popupContent">
        <div class="GAT4PNUJO" style="position: relative;">
            <div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
            <div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
                <div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
                    <div class="GAT4PNUPO GAT4PNUNO">
                        <div class="gwt-Label GAT4PNUOO">Add Project</div>
                    </div>
                    <div class="GAT4PNUMO">
                    <div class="gwt-HTML GAT4PNUIO GAT4PNUKO">An engine message has been returned. Original message: There is already a scv project with name 'test001'</div>
                    <div class="GAT4PNUHO GAT4PNULO">
                </div>
            </div>
        </div>
    </div>

Is there a way I can check if the pop up is shown and then click it in Python Webdriver?

I have read in another post someone mentioned to use Robot. I think that is Java only.

I have tried

alert = self.driver.switch_to_alert()       
alert.accept()  

just to check and prove it is not a JavaScript alert. The error shown is

NoAlertPresentException: Message: No alert is active

Upvotes: 0

Views: 4864

Answers (2)

Riaz Ladhani
Riaz Ladhani

Reputation: 4062

I have managed to solve this now. I used the following code:

self.driver.find_element_by_xpath("//*[text()='OK']").click()

The Ok button is clicked on in IE and the pop up closes.

Upvotes: 1

esoleco
esoleco

Reputation: 74

I had the same when trying to leave a page, a pop-up came and asked if I was sure I want to leave the page. I solved it with FF settings:

webdriver.DesiredCapabilities.FIREFOX["unexpectedAlertBehaviour"] = "accept"

I then catch the exception and do what I want, which is leave the site. I hope this works for you too. You have to catch the exception, because it still causes the exception to come up and stop your code.

Upvotes: 0

Related Questions