如何验证模式对话框的存在

How to verify presence of a modal dialog

我有一个网页有一个下载文件的按钮。单击该按钮后,会出现一个模态对话框弹出窗口,其中没有 'Ok' 或 'Cancel' 的按钮,它只是显示 'Generating the debuginfo file. Please wait.' 的信息对话框。在我的 ui 测试中,我需要检查该对话框是否存在。此模态对话框的 html 代码如下所示。我应该如何检查对话框的存在?

我尝试过使用 Alert class 以及 Alert(driver).switch_to.alert()Alert(driver).dismiss() 以及 accept() 我也试过

WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, "id_value")))

编辑:编辑以添加实际代码。

此元素的 html 代码具有以下参数:

     <div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable" style="position: fixed; height: auto; width: 400px; top: 143px; left: 555.5px; display: block;" tabindex="-1" role="dialog" aria-describedby="dialog" aria-labelledby="ui-id-1">
      <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>

所需元素带有 模态对话框 以便定位和提取元素文本 生成调试信息文件。请稍候。 您必须为 visibility_of_element_located() 引入 WebDriverWait 并且您可以使用以下任一项 :

  • 使用CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ui-dialog-content.ui-widget-content#dialog"))).get_attribute("innerHTML").split(">")[1])
    
  • 使用XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='ui-dialog-content ui-widget-content' and @id='dialog']"))).get_attribute("innerHTML").split(">")[1])
    
  • 注意:您必须添加以下导入:

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