1

I have to check if a change was done in the inner text of an area on my website.

Lets assume this to be the html of such page:

<span _ngcontent-c1="" class="image">
   <span _ngcontent-c1="" style="font-weight: 700">test</span>
        "Question"
   <sup _ngcontent-c1="" class="supR">®</sup></span>

I have this to find the element:

IWebElement testText= driver.FindElement(By.Class("image")).Text;

After clicking on an item in the page, the inner text "Question" changes to "Answer", for example.

What is the best way to check the change? Create a new IWebElement and compare it (with an assertion) to the first one? This is the only way I know. Although, I would like to know if there is a better way. Because in case the inner Text changes with other actions, I don't think creating a new IWebElement every time to check if the change was made.

1
IWebElement element = driver.FindElement(By.Id("element-id"));

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.TextToBePresentInElement(element, "text"));

use webdriver wait :https://www.lambdatest.com/blog/explicit-fluent-wait-in-selenium-c/

https://riptutorial.com/selenium-webdriver/example/15496/different-types-of-explicit-wait-conditions

5
  • Thank you. I had a look at those articles before (or similar). My question is still, what if the change is done by an action, like clicking on an item on a page that will make the "Inner text" change? and after, how can i compare the 2 inner texts?
    – DjNewma
    Apr 26 at 13:03
  • click on the element and wait for the new text to appear on the same element
    – PDHide
    Apr 26 at 13:09
  • if element doesn't has that string then text will fail with timeout
    – PDHide
    Apr 26 at 13:09
  • And how would i go about comparing the before and after?
    – DjNewma
    Apr 26 at 13:20
  • Just add wait for element not to contains that old text , and contains the new text
    – PDHide
    Apr 26 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.