user393267
user393267

Reputation:

Selenium-Python: interact with system modal dialogs

I am running an app in the browser; and for some actions I was able to simulate actions with keystrokes; but I have a peculiar problem: some actions in my app cause system prompt to pop up, like for example for save or confirm quit.

Is there a way to control these in Selenium python? As example imagine to use keystroke to save a page; then the system dialog (which is not part of the web app), appear and ask you where to save the file. Or imagine the prompt that ask you if you are sure to close the browser window if you have multiple tabs open.

I did try to look for a different window, assuming that I can switch context between windows in the browser, but I find nothing beside the main app, because these are system windows. Is there a workaround for this?

Upvotes: 2

Views: 6538

Answers (2)

Corey Goldberg
Corey Goldberg

Reputation: 60654

If you are talking about system dialogs, then it's not possible to interact with them using selenium.

However, for browser popups (alerts), just navigate to the popup:

driver.switch_to_alert()

Then, use the methods from the Alert class to interact with the popup. The Alert class contains methods for dismissing, accepting, inputting, and getting text from alert prompts.

Some examples:

Alert(driver).accept()
Alert(driver).dismiss()
Alert(driver).authenticate()
Alert(driver).send_keys(keys_to_send)
Alert(driver).text()

see: https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.alert.html

Upvotes: 1

aberna
aberna

Reputation: 5814

You could make usage of the Selenium methods to check current window and move to another one:

You can use

driver.window_handles

to find a list of window handles and after try to switch using following methods (selenium documentation).

  • driver.switch_to_alert()
  • driver.switch_to.active_element
  • driver.switch_to.default_content
  • driver.switch_to.window

Since the application you work on, seems to respond to Selenium commands here it is a working example about opening a popup window, switching selenium scope on it, extract data and close the popup. The process is repeated for all the products:

for item in driver.find_elements_by_class_name("products"):
    item.click() # clicking on item activate a popup
    driver.switch_to_alert() #switch to new window
    # Get data
    driver.find_elements_by_css_selector(".ui-dialog-titlebar-close.ui-corner-all")[0].click() #close window

Upvotes: 0

Related Questions