简体   繁体   中英

Catch an iframe dialog box using Selenium in Python

I am involved in a project which involves logging to a secured website and downloading a file by clicking on an element within the page which then shows a dialog box where the "OK" button has to be clicked to save the box. I have coded all the relevant steps to reach and click on the button which generates the Save File dialog box.

I have Python-3.4.1 32 bit on a Windows 7 64 bit machine with Selenium using Mozilla Firefox 33. I tried getting the code for the dialog box which is:

  <!DOCTYPE html> <html id="ext-gen3" class=" ext-strict x-viewport" decorator="blank"> <head></head> <body id="ext-gen4" class="ext-gecko3 cwc-view x-border-layout-ct" onload="cwc.getFrameworkWindow()"> <div id="cwc_header" class=" masthead x-border-panel" style="left: 0px; top: 0px; width: 1366px; height: 27px;"></div> <div id="cwcNavPanel" class=" x-panel cwc-navPanel x-border-panel" style="width: 220px; left: 0px; top: 56px;"></div> <div id="cwcCenterPanel" class=" x-tab-panel cwc-centerPanel x-border-panel" style="left: 228px; top: 35px; width: 1138px;"></div> <!--Telephony Applet --> <iframe id="ext-gen358" class="ext-shim" frameborder="0" src="/sm/js/9.33.4005/extjs/resources/images/default/s.gif" style="display: none; visibility: visible; z-index: 14998; left: 617px; top: 87px; width: 170px; height: 246px;"></iframe> <div id="ext-comp-512057" class="x-tip" style="position: absolute; z-index: 20002; visibility: hidden; width: 74px; left: 672px; top: 89px; display: none;"></div> <div id="cwcNavPanel-xsplit" class="x-layout-split x-layout-split-west x-unselectable x-splitbar-h" style="left: 220px; top: 56px; height: 351px;"></div> <div id="ext-gen54" class=" x-unselectable x-splitbar-proxy x-splitbar-proxy-h"></div> <div id="ext-gen359" style="position: absolute; left: -1000px; top: -1000px; visibility:…; text-transform: none; letter-spacing: normal; width: auto;"></div> <div id="ext-gen76" class="x-shadow" style="z-index: 20000; left: 634px; top: 185px; width: 98px; height: 38px; display: none;"></div> </body> </html> 

How do I catch the alert box that pops up?

I have tried doing:

try:
    alert = driver.switch_to_alert()
    alert.accept()
except Exception as E:
    print("Exception occured:", E)

But it always gives me the exception that no alert found. What if the dialog box that appears is an "iframe" instead.

Thank you for your help!

I am using sufficient amount of time in seconds to wait for the dialog box to occur before i try and catch it using "alerts"

After switching to the dialog box using,

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

(which i am not sure if it actually did select the dialog box and I don't know how to select it)

how do I click "OK" to save the file. I am not able to view this dialog box's source code.

Thanks!!

#for iframes:
driver.switch_to_frame(driver.find_element_by_tag_name("iframe")) #switching to iframe
element = driver.find_elements_by_css_selector(".some_inside_iframe")
#do something

driver.switch_to_default_content() #back into default content

if you need to handle Alert in driver, you can check:

from selenium.webdriver.support import expected_conditions 

and pass "alert is present" condition

Also, if alert takes some time you can do it before alert is opening:

import time

time.sleep(3) #allow driver to wait before alert is present
#you code for alert here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM