-2

I have an element within tag that has release notes. I have to validate if it contains specific text. I am able to extract the text using following code:

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

How do I assert if it contain a specific text, say "abc". Is there any function like contains() or isPresent() that I can use here?

The code that I am working on is:

<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>
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
nd23
  • 73
  • 5
  • 1
    yes, but i don't know if its possible without using the xpath functions in selenium. The following would find a pre that contains "Text is here.": `driver.find_element_by_xpath("//pre[contains(., 'Text is here.')]")` – Jimmy Engelbrecht Jun 05 '19 at 19:59

1 Answers1

0

To validate the presence of the desired text within any element you need to use a try-catch{} block inducing WebDriverWait for text_to_be_present_in_element() and you can use either of the following Locator Strategies:

  • Using 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")
    
  • Using 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")
    
  • 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
    from selenium.common.exceptions import TimeoutException
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Thank you for your response. It worked for me. The only thing is the release notes that I am validating is too long and if I validate for a failure of test with a wrong keyword that does not exist then it throws timeout exception. And I am not sure if there are ways to speed up the search process to give a failure. Also just to confirm the solution that you gave is checking if the expected text is contained in the release notes? that is validate partial text match? – nd23 Jun 06 '19 at 13:42
  • @nd23 [Upvote](https://stackoverflow.com/help/why-vote) the answer if this/any answer is/was helpful to you for the benefit of the future readers. – DebanjanB Jun 06 '19 at 13:44
  • @nd23 You can always replace the string `Text is here` with a variable containing a smaller/larger text. – DebanjanB Jun 06 '19 at 13:46
  • Sorry I didn't quite understand this comment. In my solution I am using this: ```element = WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, "div.ui-dialog-content.ui-widget-content#dialog>pre"), "Text"))``` to validate that it is contained within "Text is here". What I meant was if I check for a test failure it takes too long is there a way to make the search faster? – nd23 Jun 06 '19 at 13:58
  • @nd23 Checkout the updated answer and let me know if you have further queries. – DebanjanB Jun 06 '19 at 14:09
  • Thank you. This is not exactly what I though could be alternate solution in selenium but I can use this to get rid of timeout exception. – nd23 Jun 06 '19 at 14:36