Keegan Husom
Keegan Husom

Reputation: 141

Accepting an IE Webpage Dialog box with Selenium

I'm trying to do automation for a webpage that only works in IE. I sign into the application and there is a "-- Webpage Dialog" box that appears and says PROD and has the option to click OK. It will not allow me to continue unless I click OK. I can't figure out how to accept that box. I can't inspect it. I've tried to turn on the url for pop ups but my company will not allow me to do so. It's frustrating because all I have to do is click ENTER and it works but the automation just can't send a send_keys(Keys.ENTER). I have also tried to switch windows but there is only one window. How can I accept this box using selenium?enter image description here

I have tried driver.switch_to_alert().accept() or different variations. It doesn't do anything after that line has been executed. The program thinks it performed the execution but when in fact it didn't.

Upvotes: 1

Views: 2798

Answers (1)

Zhi Lv
Zhi Lv

Reputation: 21581

Whenever an Alert gets triggered and a pop-up appears on the web page, the control remains with the parent web page. So first we need to switch or transfer the control to alert pop-up before doing any operations.

This control switching operation can be done by using anyone of below two code snippets.

alert = driver.switch_to.alert

then, handle the alert using the following command:

alert.accept() – Will click on OK button

More detail, please check this article.

Edit:

After checking the screenshot, I think perhaps you are using window.showModalDialog() method to display the popup window, instead of an Alert. So, we could not use the alert.accept() method to click the "OK" button. You could use F12 developer tools to check the Html element to verify whether it is a web page or not.

Since, it is a popup window to display a web page, you could switch to the popup window, then, using the find_element_by_id() to find the "OK" button, after that, we could click the button to close the popup window.

You could refer to the following code:

Code in the web page (display a webpage dialog):

<button id='show-windowdialog' onclick='window.showModalDialog("About.aspx", window)'>Open Webpage Dialog</button>

python code:

from selenium import webdriver
driver = webdriver.Ie("D:\\Downloads\\webdriver\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe")

# connect to the specific ip address
driver.get("http://localhost:54382/pythondhtmlpage.html")

driver.find_element_by_id("show-windowdialog").click()
# find the current window
main_page = driver.current_window_handle

handles = driver.window_handles
# print the window_handle length     
print(len(handles))

popup_window_handle = None
# loop through the window handles and find the popup window.
for handle in driver.window_handles:
    if handle != main_page:
        print(handle)
        popup_window_handle = handle
        break
# switch to the popup window.
driver.switch_to.window(popup_window_handle)
# trigger the close button to close the popup window.
driver.find_element_by_id("closewindow").click()

# Finally, switch to the main page.
driver.switch_to.window(main_page)

Then screenshot as below:

enter image description here

Upvotes: 1

Related Questions