简体   繁体   English

如何关闭或关闭模式对话框

[英]How to close or dismiss a modal dialog

I have a modal dialog which I need to close/dismiss after I have identified it's presence and extracted it's text. 我有一个模式对话框,在确定它的存在并提取其文本后,需要关闭/关闭它。 The modal comes up when I click on the button to download a file and it says "Generating the debuginfo file. Please wait." 当我单击按钮以下载文件时,该模式出现,并显示“正在生成debuginfo文件。请稍候。” This modal is present until the file download starts which is too long depending upon the debuginfo file size. 该模式一直存在,直到文件下载开始,该时间过长,具体取决于debuginfo文件的大小。 Is there a way to close this dialog and exit with text extracted from it. 有没有办法关闭此对话框并退出并提取其中的文本。

I am extracting the text using following code: 我正在使用以下代码提取文本:

downloadText = WebDriverWait(self.driver,40).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ui-dialog-content.ui-widgetcontent#dialog"))).get_attribute("innerHTML").split(">")[1]

Following is the html code for this modal: 以下是此模式的html代码:

     <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
     <span id="ui-id-1" class="ui-dialog-title">Download debug info</span>
     <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" aria-disabled="false" title="close">
       <span class="ui-button-icon-primary ui-icon ui-icon-closethick">
       </span>
       <span class="ui-button-text">close</span>
     </button>
   </div>
   <div id="dialog" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: 177px;">
     <br>Generating the debuginfo file. Please wait.</div>
     <div class="ui-resizable-handle ui-resizable-n" style="z-index: 90;">
     </div>
     <div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;">
     </div>
     <div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;">
     </div>
     <div class="ui-resizable-handle ui-resizable-w" style="z-index: 90;">
     </div>
     <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;">
     </div>
     <div class="ui-resizable-handle ui-resizable-sw" style="z-index: 90;">
     </div>
     <div class="ui-resizable-handle ui-resizable-ne" style="z-index: 90;">
     </div>
     <div class="ui-resizable-handle ui-resizable-nw" style="z-index: 90;">

 </div> ```

Based on the sample code provided in the Original Post, you should be able to close the dialog with the below xpath. 根据原始帖子中提供的示例代码,您应该能够使用下面的xpath关闭对话框。

 //span[@class='ui-button-icon-primary ui-icon ui-icon-closethick']

Here is the script to click on the element even if it's not visible. 这是即使元素不可见也可以单击的脚本。

ele = driver.find_element_by_xpath("//span[@class='ui-button-icon-primary ui-icon ui-icon-closethick']")
driver.execute_script("arguments[0].click();",ele)

The close the Modal Dialog Box you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies : 关闭要为element_to_be_clickable()引发WebDriverWait的“ 模态”对话框,并且可以使用以下定位策略之一

  • Using CSS_SELECTOR : 使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-icon-only.ui-dialog-titlebar-close[title='close'] span.ui-button-icon-primary.ui-icon.ui-icon-closethick"))).click() 
  • Using XPATH : 使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close' and @title='close']//span[@class='ui-button-icon-primary ui-icon ui-icon-closethick']"))).click() 
  • Note : You have to add the following imports : 注意 :您必须添加以下导入:

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

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

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