How can I interact with this modal dialog using Webdriver & Python?

Wilco van Esch picture Wilco van Esch · Apr 11, 2013 · Viewed 10.7k times · Source

All I want is to close a modal dialog, ideally by doing the following:

browser.find_element_by_link_text("OK").click()

Gives NoSuchElementException: Message: u'The element could not be found' for the OK link text.

Same for the xpath when I do this:

browser.find_element_by_xpath("//*[@id=\"modal\"]/div/div[2]/div/a").click()

I suspect this is because I need to put focus on the dialog. To do so I've tried:

for handle in browser.window_handles:
    browser.switch_to_window(handle)
    if browser.find_element_by_class_name('popUp123')
        browser.find_element_by_link_text("OK").click()

Gives NoSuchElementException: Message: u'The element could not be found' for the class.

Have also tried browser.switch_to_frame(ID OR NAME), but couldn't find it as a frame either.

Please tell me I'm missing something blatantly obvious.

Relevant frame source (summarised):

<body id="modal">
    <div class="popUp123">
    <div class="button">
        <div class="centerbutton">
            <a href="#" class="close" onclick=parent.close">
                <span>OK</span>

Answer

Brion Finlay picture Brion Finlay · Dec 2, 2013

This is the python syntax

from selenium.webdriver.remote.webdriver import WebDriver

browser = WebDriver()

# do other stuff here

browser.switch_to_alert().accept()

# continue with other stuff here

The alert api is located in selenium.webdriver.common.alert