简体   繁体   English

在 Python 中使用 Selenium 关闭 Modal

[英]Closing Modal using Selenium in Python

I've been trying to close a modal for some time now and somehow nothing works.一段时间以来,我一直在尝试关闭模态,但不知何故没有任何效果。

The last thing I tried was executing a JS script.我尝试的最后一件事是执行 JS 脚本。 It works if I execute it in the browser console but not with selenium.如果我在浏览器控制台中执行它而不是使用 selenium 执行它,它会起作用。

I used:我用了:

driver.execute_script("document.getElementById('btnChiudi').click()

to close:关闭:

        <div id="ctl00_mainCopy_upMsgFePa">
                <div tabindex="-1" id="Modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">
                                    <span class=" glyphicon glyphicon-info-sign"><input type="image" name="ctl00$mainCopy$CustomImageButton1" id="ctl00_mainCopy_CustomImageButton1" disabled="disabled" src="" style="border-width:0px;"></span>
                                    <span id="ctl00_mainCopy_Label1">Avviso</span></h5><button type="button" class="btn--close close" data-dismiss="modal" aria-label="Close" id="btnCloseX" onclick="$get('btnChiudi').click()">
                                    <span aria-hidden="true">×</span></button></div><div class="modal-body">
                                <center>
                                    <span id="ctl00_mainCopy_msgAttenzioneFePa" style="display:inline-block;border-width:0px;border-style:None;font-size:11pt;width:97%;text-align: left">TEXT<ul id="ctl00_mainCopy_blListConvInComp" style="text-align: left; padding-left: 25px;"><li>TEXT</li></ul>TEXT</span><table id="ctl00_mainCopy_TblElencoConvenzioniSenzaSceltaFePa" width="100%">
                                    <tbody><tr>
                                        <td>&nbsp; </td>
                                    </tr>
                                    <tr>
                                        <td>
                                             <ul id="ctl00_mainCopy_blListConvInComp" style="text-align: left; padding-left: 25px;">
                                                  <li>SSP00703052</li>
                                             </ul>
                                        </td>
                                    </tr>
                                    </tbody></table>
                                </center>
                            </div>
                            <div class="modal-footer">
                                <input type="submit" name="ctl00$mainCopy$btnChiudi" value="Chiudi" id="btnChiudi" class="btn btn--secondary" data-dismiss="modal">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

modal模态

You may want to try with below xpath with Explicit waits like below :您可能想尝试使用下面的 xpath 和显式等待,如下所示:

XPATH :路径:

//input[@id='btnChiudi']/..

code :代码 :

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='btnChiudi']/.."))).click()

Imports :进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

它通过获取窗口并在它们之间切换来工作,因为引导程序实际上并不生成真正的弹出窗口,而是使用不同的窗口来显示模式,您可以使用以下代码在它们之间切换: Python webdriver to handle pop up browser windows which is not an alert

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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