# Not able to get correct text of an element

**URL:** <https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [June 21, 2019, 6:09pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407 "2019-06-21T18:09:40Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [June 21, 2019, 6:09pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/1 "2019-06-21T18:09:40Z")

</div>

Hello Team,

I have an element for which I am trying to verify Text but not able to do so.

The script is something like this.

```auto
WebUI.verifyElementPresent(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), 0)

Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE)

CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'('Size Guide', Size_Guide_Header_Text, true, 'To Test the Size Guide Header Text', 
    FailureHandling.STOP_ON_FAILURE)

```

The error I am getting is :

```auto
06-21-2019 10:43:10 AM Test Cases/QATB-182 - Size Guide Testing/QATB-182 - Automate Testing of Size Guide

Elapsed time: 16.465s

kms.turing.katalon.plugins.assertj.GenericAssert.handleError:71

kms.turing.katalon.plugins.assertj.GenericAssert.handleError:85

kms.turing.katalon.plugins.assertj.StringAssert.equals:103

kms.turing.katalon.plugins.assertj.GenericAssert.invokeMethod:0

Test Cases/QATB-182 - Size Guide Testing/QATB-182 - Automate Testing of Size Guide FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: To Test the Size Guide Header Text.
Expected: equals to '' with case-sensitive: true.
Actual: Size Guide

```

From the error, it looks like the selector of the element is not able to get the text correctly

 ![44%20AM](https://europe1.discourse-cdn.com/katalon/original/2X/6/65a86e781e5cfd7f0a09ab6fb6c537da9bba3003.png)

---

<div class="post-metadata">

**Author:** ![ThanhTo](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/thanhto/32/697_2.png) [@ThanhTo](https://forum.katalon.com/u/ThanhTo)\
**Post date:** [June 21, 2019, 6:12pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/2 "2019-06-21T18:12:44Z")

</div>

> [@manpreet.mukkar](#):
>
> com.kms.katalon.core.exception.StepFailedException: To Test the Size Guide Header Text. Expected: equals to ‘’ with case-sensitive: true. Actual: Size Guide

You’re expecting a different text, the actual text is 'Size Guide’s which is what you want. You should check the assertion to see if the expecting text is indeed ‘Size Guide’.

Cheers !

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [June 21, 2019, 7:08pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/3 "2019-06-21T19:08:37Z")

</div>

@ThanhTo I fixed that part sorry it was my mistake, Now the code is something like this

```auto
/* Clicking on Size Guide */
WebUI.click(findTestObject('Page_PDP/Size_Guide'))

WebUI.verifyElementPresent(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), 0)

Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE)

CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'(Size_Guide_Header_Text, 'Size Guide', true, 'To Test the Size Guide Header Text', 
    FailureHandling.STOP_ON_FAILURE)

```

I am still getting error :

```auto

Test Cases/QATB-182 - Size Guide Testing/QATB-182 - Automate Testing of Size Guide FAILED.
Reason:
com.kms.katalon.core.exception.StepFailedException: To Test the Size Guide Header Text.
Expected: equals to 'Size Guide' with case-sensitive: true.
Actual: 
	at com.kms.katalon.core.util.KeywordUtil.markFailedAndStop(KeywordUtil.java:28)
	at com.kms.katalon.core.util.KeywordUtil$markFailedAndStop$1.call(Unknown Source)
	at kms.turing.katalon.plugins.assertj.GenericAssert.handleError(GenericAssert.groovy:71)
	at kms.turing.katalon.plugins.assertj.GenericAssert.handleError(GenericAssert.groovy:85)
	at kms.turing.katalon.plugins.assertj.StringAssert.equals(StringAssert.groovy:103)
	at kms.turing.katalon.plugins.assertj.GenericAssert.invokeMethod(GenericAssert.groovy)
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
	at QATB-182 - Automate Testing of Size Guide.run(QATB-182 - Automate Testing of Size Guide:89)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1561143993249.run(TempTestCase1561143993249.groovy:21)

```

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [June 25, 2019, 4:24pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/4 "2019-06-25T16:24:03Z")

</div>

You probably need to trim the text you retrieve from the application:

`Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE).trim()`

This is because there’s a bunch of whitespace in the text node:

![image](https://europe1.discourse-cdn.com/katalon/original/2X/2/2a0aafc0174bf2cd394b74ebe2245aa6a54b9275.png)

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [June 25, 2019, 4:45pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/5 "2019-06-25T16:45:54Z")

</div>

@Brandon_Hein I tried but it is giving the same error.

```auto
<span class="fit-title" xpath="1">
                                Size Guide
                            </span> 

```

Code :

```auto
WebUI.verifyElementPresent(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), 0)

Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE).trim()

CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'(Size_Guide_Header_Text, 'Size Guide', true, 'To Test the Size Guide Header Text', 
    FailureHandling.STOP_ON_FAILURE)

```

Error :

```auto
06-25-2019 09:44:06 AM kms.turing.katalon.plugins.assertj.StringAssert.equals(Size_Guide_Header_Text, "Size Guide", true, "To Test the Size Guide Header Text", STOP_ON_FAILURE)

Elapsed time: 0.012s

To Test the Size Guide Header Text.
Expected: equals to 'Size Guide' with case-sensitive: true.
Actual: 

```

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [June 25, 2019, 4:48pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/6 "2019-06-25T16:48:25Z")

</div>

Ok we need to see what is being returned by the getText() call. Please print the value after it is retrieved:

```
Size_Guide_Header_Text = WebUI.getText(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), FailureHandling.STOP_ON_FAILURE).trim()
System.out.println(Size_Guide_Header_Text)

```

Also, the

`CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'()`

is your custom implementation, so it may not be working as you expect. Can you share the code for this keyword?

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [June 25, 2019, 5:01pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/7 "2019-06-25T17:01:10Z")

</div>

I just realized that the

`CustomKeywords.'kms.turing.katalon.plugins.assertj.StringAssert.equals'()`

probably isn’t a keyword that you wrote, but rather a keyword from a plugin. As such, try just doing a simple assertion like this, and see if it makes any difference:

`assert Size_Guide_Header_Text.equals("Size Guide")`

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [June 25, 2019, 5:06pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/8 "2019-06-25T17:06:38Z")

</div>

@Brandon_Hein It looks like there is a line break in the element.

The console log looks like this when I printed the text from getText() call

```auto
2019-06-25 10:03:58.387 DEBUG ATB-182 - Automate Testing of Size Guide - 18: Size_Guide_Header_Text = getText(findTestObject("Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title"), STOP_ON_FAILURE).trim()
2019-06-25 10:03:58.426 DEBUG ATB-182 - Automate Testing of Size Guide - 19: out.println(Size_Guide_Header_Text)

2019-06-25 10:03:58.435 DEBUG ATB-182 - Automate Testing of Size Guide - 20: assert Size_Guide_Header_Text.equals("Size Guide")
2019-06-25 10:03:58.440 ERROR c.k.katalon.core.main.TestCaseExecutor - ❌ Test Cases/QATB-184 - Size Guide Testing/QATB-182 - Automate Testing of Size Guide FAILED.
Reason:
Assertion failed: 

assert Size_Guide_Header_Text.equals("Size Guide")
       | |
       "" false

	at QATB-182 - Automate Testing of Size Guide.run(QATB-182 - Automate Testing of Size Guide:105)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1561482213510.run(TempTestCase1561482213510.groovy:21)

```

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [June 25, 2019, 5:09pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/9 "2019-06-25T17:09:13Z")

</div>

Hmmm it’s not even a line break, it’s an empty string, as shown in the assertion:

```
assert Size_Guide_Header_Text.equals("Size Guide")
       | |
       "" false

```

Are you sure you are targeting the right element?

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [June 25, 2019, 5:40pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/10 "2019-06-25T17:40:59Z")

</div>

@Brandon_Hein I think so,

I have xpath set as

 ![17%20AM](https://europe1.discourse-cdn.com/katalon/original/2X/c/c7aa536c7a7441d0ebbfcf67e06f746f29aa7213.png)

 ![03%20AM](https://europe1.discourse-cdn.com/katalon/original/2X/5/5411ef379c273db6a71bcf32720839bc93268964.png)

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [June 25, 2019, 5:43pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/11 "2019-06-25T17:43:14Z")

</div>

That xpath may be returning multiple elements. Test it in your browser console by hitting F12, going to the “Console” tab, enter the following, and see what you get back:

`$x("//*[@class='fit-title']")`

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [June 25, 2019, 5:53pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/12 "2019-06-25T17:53:32Z")

</div>

You are right, it is returning 3 elements.

 ![04%20AM](https://europe1.discourse-cdn.com/katalon/original/2X/7/789d4e00f06efabc7df92e94b534fb91ddcd0edc.png)

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [June 25, 2019, 5:56pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/13 "2019-06-25T17:56:19Z")

</div>

Yea, but in this case that doesn’t matter, the one you want is the first one that is being returned anyway (you should definitely be careful of this in general though). Try this instead and see what you get:

```
Size_Guide_Header_Text = WebUI.getAttribute(findTestObject('Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title'), "textContent", FailureHandling.STOP_ON_FAILURE).trim()
System.out.println(Size_Guide_Header_Text)
```

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [June 25, 2019, 6:03pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/14 "2019-06-25T18:03:16Z")

</div>

@Brandon_Hein I tried this approach with the same selector and it worked.

```auto
2019-06-25 11:01:35.991 DEBUG ATB-182 - Automate Testing of Size Guide - 17: Size_Guide_Header_Text = getAttribute(findTestObject("Page_PDP/Size_Guide_Pop_Up/Size_Guide_Title"), "textContent", STOP_ON_FAILURE).trim()
2019-06-25 11:01:36.035 DEBUG ATB-182 - Automate Testing of Size Guide - 18: out.println(Size_Guide_Header_Text)
Size Guide
2019-06-25 11:01:36.045 DEBUG ATB-182 - Automate Testing of Size Guide - 19: assert Size_Guide_Header_Text.equals("Size Guide")

```

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [June 25, 2019, 6:04pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/15 "2019-06-25T18:04:28Z")

</div>

Nice, so your assertion should work now. Not sure why WebUI.getText() wasn’t doing what you expected (because I never use the WebUI API). I find that getting the `textContent` attribute value is often more reliable than using any kind of getText() method, but that’s just me 😄

---

<div class="post-metadata">

**Author:** ![manpreet.mukkar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/manpreet.mukkar/32/16103_2.png) [@manpreet.mukkar](https://forum.katalon.com/u/manpreet.mukkar)\
**Post date:** [June 25, 2019, 6:06pm UTC](https://forum.katalon.com/t/not-able-to-get-correct-text-of-an-element/28407/16 "2019-06-25T18:06:42Z")

</div>

@Brandon_Hein Thanks for your help again. It is always something new to learn from you. I do have some other areas with the same issue and I think I can use this approach.
