Search code examples
pythongoogle-chromewebdrivermodal-dialoglettuce

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


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>

Solution

  • The below code is using Java, u can try using the below code converting it to Python syntax. Sorry as I am Webdriver - Java Tester, I can't give you the Python code. Hope this will solve your requirements.

    Alert alertDialog = driver.switchTo().alert();
    //Get the alert text
    String alertText = alertDialog.getText();
    //Click the OK button on the alert.
    alertDialog.accept();
    

    Cheers,

    Mahesh