我参与了一个项目,该项目涉及登录到安全的网站并通过单击页面中的元素下载文件,然后显示一个对话框,其中" OK"必须单击按钮才能保存该框。我编写了所有相关步骤,然后单击生成“保存文件”对话框的按钮。
我在使用Mozilla Firefox 33的Windows 7 64位计算机上安装了Python-3.4.1 32位。我尝试获取对话框的代码:
<!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>
&#13;
如何捕捉弹出的警告框?
我尝试过:
try:
alert = driver.switch_to_alert()
alert.accept()
except Exception as E:
print("Exception occured:", E)
但它始终给我一个例外,即没有发现警报。如果出现的对话框是&#34; iframe&#34;代替。
感谢您的帮助!
我在几秒钟内使用足够的时间等待对话框发生,然后我尝试使用&#34;警告&#34;
抓住它使用后切换到对话框,
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
(我不确定它是否确实选择了对话框而且我不知道如何选择它)
如何点击&#34;确定&#34;保存文件。我无法查看此对话框的源代码。
谢谢!
答案 0 :(得分:0)
#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
如果您需要在驱动程序中处理警报,您可以检查:
from selenium.webdriver.support import expected_conditions
并传递&#34;警报存在&#34;条件
此外,如果警报需要一些时间,您可以在警报打开之前执行此操作:
import time
time.sleep(3) #allow driver to wait before alert is present
#you code for alert here