断言元素内的文本是否包含特定的部分文本

时间:2019-06-05 19:51:53

标签: python selenium xpath css-selectors webdriverwait

标签中有一个具有发行说明的元素。我必须验证它是否包含特定文本。我可以使用以下代码提取文本:

WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.XPATH, ManageSoftware.release_notes_xpath))).get_attribute("innerHTML")

如何断言它是否包含特定文本,例如“ abc”。我可以在这里使用任何包含contains()或isPresent()的函数吗?

我正在处理的代码是:

<div id="dialog" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: 483px;">
<pre> 
Text is here.
</pre>
</div>

1 个答案:

答案 0 :(得分:1)

要验证所需文本是否存在于任何元素中,您需要使用try-catch{}的{​​{1}}块诱导 WebDriverWait ,并且可以使用以下{{ 3}}:

  • 使用text_to_be_present_in_element()

    CSS_SELECTOR
  • 使用try: WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, "div.ui-dialog-content.ui-widget-content#dialog>pre"), "Text is here")) print("Desired text was present") except TimeoutException: print("Desired text was not present")

    XPATH
  • 注意:您必须添加以下导入:

    try:
        WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.XPATH, "//div[@class='ui-dialog-content ui-widget-content' and @id='dialog']/pre"), "Text is here"))
        print("Desired text was present")
    except TimeoutException:
        print("Desired text was not present")