【问题标题】:How to verify presence of a modal dialog如何验证模态对话框的存在
【发布时间】:2019-10-17 04:33:33
【问题描述】:

我有一个网页,上面有一个下载文件的按钮。单击按钮后,会出现一个模式对话框弹出窗口,其中没有“确定”或“取消”按钮,它只是显示“生成调试信息文件”的信息对话框。请稍等。'。在我的 ui 测试中,我需要检查该对话框是否存在。此模式对话框的 html 代码如下所示。我应该如何检查对话框的存在?

我尝试过使用 Alert 类并使用 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>

【问题讨论】:

  • 用实际的 HTML 更新问题,而不是手工制作的 HTML。
  • 添加了实际代码。
  • 我可以看到文本下载调试信息生成调试信息文件。请稍候。但我在您提供的 HTML 中没有看到 your download will begin... 文字。你可以交叉检查一次吗?
  • 对不起,我的错。当我第一次发布问题时,我实际上并没有查看代码。再次更新我的问题。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

所需的元素带有一个模态对话框,以便定位和提取元素文本生成调试信息文件。请稍候。您必须为visibility_of_element_located() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

  • 使用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
    

【讨论】:

  • 感谢您的回复。 css_selector 方法对我有用。但是,当我使用 splitlines()[2] 时,我收到错误“IndexError: list index out of range”,所以我使用了 splitlines()[1] 并得到了 TimeoutException。同样不使用分割线,我得到的文本返回为:
    Generating the debuginfo file。请稍等。有没有办法删除
    标签?最后一个问题,一旦我们检测到它的存在,我们可以关闭对话框吗?目前对话框一直打开,直到下载开始,然后测试完成。
  • 接受了您的回答。还编辑了我上面的评论,以包含有关该错误的更多信息。
  • @nd23 查看更新的答案并让我知道状态。
  • 您更新的解决方案稍作更改,我尝试使用 split(">")[1] 而不是索引 2。谢谢。一旦我们检测到innerHTML,有没有办法关闭这个模式?
  • @nd23 听起来不错!!!你能根据你的新要求提出一个新问题吗?
猜你喜欢
  • 2015-11-24
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 2014-11-13
  • 1970-01-01
相关资源
最近更新 更多