4

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>
1
  • If the error does not appear if you put a time.sleep (2) befor the code you may need to wait for the element. Commented Apr 11, 2013 at 17:48

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.