Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 992
Wilco van Esch Avatar asked Mar 24 '23 08:03

Wilco van Esch


1 Answers

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

like image 196
Brion Finlay Avatar answered Apr 19 '23 09:04

Brion Finlay