# Get Text from Page and Store in Variable

**URL:** <https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [September 8, 2017, 5:42pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587 "2017-09-08T17:42:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Kraft](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Josh\_Kraft](https://forum.katalon.com/u/Josh_Kraft)\
**Post date:** [September 8, 2017, 5:42pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/1 "2017-09-08T17:42:16Z")

</div>

How do I grab text from an element on the page, store that in a variable, and then add it to a url to navigate to?

---

<div class="post-metadata">

**Author:** ![Pooja\_Purohit](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@Pooja\_Purohit](https://forum.katalon.com/u/Pooja_Purohit)\
**Post date:** [September 9, 2017, 2:54pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/2 "2017-09-09T14:54:10Z")

</div>

Hi Josh,

Below is the script,Hope this works for you.  
Fetch Feature element from page and navigate to Feature page  
[https://katalon.com/Feature](https://katalon.com/Feature)

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘[https://katalon.com/](https://katalon.com/)’)

WebUI.verifyElementPresent(findTestObject(‘Page\_Katalon Studio - Simple Power/a\_Features’), 7)

attribute = WebUI.getAttribute(findTestObject(‘Page\_Katalon Studio - Simple Power/a\_Features’), ‘text’)

println(attribute)

Strattribute=WebUI.concatenate([‘[https://katalon.com/](https://katalon.com/)’, attribute] as String[],  
FailureHandling.STOP\_ON\_FAILURE)

println(Strattribute)

WebUI.navigateToUrl(Strattribute)

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar\_Bhukya](https://avatars.discourse-cdn.com/v4/letter/k/22d042/32.png) [@Kiran\_Kumar\_Bhukya](https://forum.katalon.com/u/Kiran_Kumar_Bhukya)\
**Post date:** [January 2, 2018, 11:54am UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/3 "2018-01-02T11:54:54Z")

</div>

WebUI.verifyElementPresent(findTestObject(‘Page\_Katalon Studio - Simple Power/a\_Features’), 7)

what is 7 in the above command

---

<div class="post-metadata">

**Author:** ![Zarashima](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/zarashima/32/928_2.png) [@Zarashima](https://forum.katalon.com/u/Zarashima)\
**Post date:** [January 4, 2018, 4:20am UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/4 "2018-01-04T04:20:25Z")

</div>

Hi Kiran,

7 is the timeout value, which means it will wait for up 7 seconds until the image is loaded. You can see its documentation here:  
[https://docs.katalon.com/display/KD/[WebUI]+Verify+Image+Present](https://docs.katalon.com/display/KD/%5BWebUI%5D+Verify+Image+Present)

---

<div class="post-metadata">

**Author:** ![Zlatan\_Midzic](https://avatars.discourse-cdn.com/v4/letter/z/ba9def/32.png) [@Zlatan\_Midzic](https://forum.katalon.com/u/Zlatan_Midzic)\
**Post date:** [June 12, 2018, 6:08pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/5 "2018-06-12T18:08:11Z")

</div>

Thank you for great answer!

---

<div class="post-metadata">

**Author:** ![Boris\_Faitout](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/boris_faitout/32/456_2.png) [@Boris\_Faitout](https://forum.katalon.com/u/Boris_Faitout)\
**Post date:** [August 22, 2018, 8:04am UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/6 "2018-08-22T08:04:04Z")

</div>

This type of request is going trendy, and the following feature may suits your needs. If you think it does, you may upvote it for Katalon guys to add it in a further release :

> **[Write into excel at runtime during test execution.](http://forum.katalon.com/discussion/4203)**
>
> Currently Katalon provides an inbuilt featute to read data from exel. But doesn't have option to write into excel.

---

<div class="post-metadata">

**Author:** ![elizabeth\_g](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/elizabeth_g/32/16103_2.png) [@elizabeth\_g](https://forum.katalon.com/u/elizabeth_g)\
**Post date:** [September 27, 2019, 10:07am UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/7 "2019-09-27T10:07:33Z")

</div>

Hey katalon team,  
I’m getting `unexpected token:Web.UI` error in script for line WebUI.navigateToUrl(Strattribute) and for any line below line println(strattribute).

Please help to solve my problem.

---

<div class="post-metadata">

**Author:** ![Peter\_Wilson](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@Peter\_Wilson](https://forum.katalon.com/u/Peter_Wilson)\
**Post date:** [September 27, 2019, 2:48pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/8 "2019-09-27T14:48:23Z")

</div>

Do you have a couple lines of sample code to help explain what is happening and variables are declared?

---

<div class="post-metadata">

**Author:** ![ehernandez](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@ehernandez](https://forum.katalon.com/u/ehernandez)\
**Post date:** [September 27, 2019, 3:27pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/9 "2019-09-27T15:27:13Z")

</div>

I believe you may be missing your WebUI import  
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

Oh also, its WebUI not Web.UI

I think thats it

---

<div class="post-metadata">

**Author:** ![elizabeth\_g](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/elizabeth_g/32/16103_2.png) [@elizabeth\_g](https://forum.katalon.com/u/elizabeth_g)\
**Post date:** [September 27, 2019, 3:30pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/10 "2019-09-27T15:30:22Z")

</div>

@Peter_Wilson should I put this part of code to test case script or it’s a part of script in Keywords? If it relates to the Keywords, how should I call it in test case.

I’m intending to get copy of URL that is generated on the page in the field and I want to insert this to the browser.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ehernandez](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@ehernandez](https://forum.katalon.com/u/ehernandez)\
**Post date:** [September 27, 2019, 3:35pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/11 "2019-09-27T15:35:56Z")

</div>

I assume you meant to tag me (based on your response)  
You would put the WebUI import in whichever script is using the WebUI. Based on what you said, I believe what you are trying to do is

1. Navigate to a page
2. Find a text element on the page that has the text of the URL, assign that URL to a variable
3. Navigate to that URL

is this correct?

---

<div class="post-metadata">

**Author:** ![elizabeth\_g](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/elizabeth_g/32/16103_2.png) [@elizabeth\_g](https://forum.katalon.com/u/elizabeth_g)\
**Post date:** [September 27, 2019, 3:41pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/12 "2019-09-27T15:41:25Z")

</div>

thanks for your reply.  
Yes, that’s correct.

I’m having every time different url generated, so need to assign to the variable to get a correct navigation in browser.  
But a bit lost with a script in test case and in keywords.

---

<div class="post-metadata">

**Author:** ![Peter\_Wilson](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@Peter\_Wilson](https://forum.katalon.com/u/Peter_Wilson)\
**Post date:** [September 27, 2019, 4:59pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/13 "2019-09-27T16:59:26Z")

</div>

It would look something similar to:

```
String url = WebUI.getUrl()
WebUI.navigateToUrl(url)

```

Or to use a Global Variable  
`WebUI.navigateToUrl(GlobalVariable.url)`

You would also be able to use other command like SetText, and do comparisons within IF statements.  
If you set the `url` to a Global Variable, you Keyword will also be able to see, use and set that value.

This would normally appear within a Test Case. But the concept is applicable to Keywords if that is the route you are taking.

---

<div class="post-metadata">

**Author:** ![josh.meeks](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/josh.meeks/32/25928_2.png) [@josh.meeks](https://forum.katalon.com/u/josh.meeks)\
**Post date:** [November 17, 2022, 2:59pm UTC](https://forum.katalon.com/t/get-text-from-page-and-store-in-variable/6587/14 "2022-11-17T14:59:22Z")

</div>

I am trying to do the same but what I have seen above does not look like what I have written. I keep getting a response of 0 from my comment(size().toString())

```auto
WebDriver driver1 = DriverFactory.getWebDriver()
List<WebElement> stagerows = driver1.findElements(By.xpath("//mat-row"))
List<WebElement> stages = driver1.findElements(By.className("mat-cell cdk-cell cdk-column-stage-displayName mat-column-stage-displayName ng-star-inserted"))
WebUI.comment(stages.size().toString())
def enabledStageNames = []
for (WebElement stagerow in stagerows) {
Boolean isEnabled = stagerow.findElements(By.className("mat-checkbox-checked")).size() > 0
  if (isEnabled==true) {
	  List<WebElement> stageDisplaynameCell = stagerow.findElements(By.className("mat-cell cdk-cell cdk-column-stage-displayName mat-column-stage-displayName ng-star-inserted"))
	def stageName = stageDisplaynameCell.getText()
	enabledStageNames.add(stageName)
  }
}
WebUI.navigateToUrl(GlobalVariable.DEVURL)
WebUI.delay(8)
WebUI.comment(enabledStageNames.size().toString())
for (String stageName in enabledStageNames){
	
	WebUI.verifyTextPresent(stageName, false)

```

Any help anyone could offer would be great.
