Get Text from Page and Store in Variable

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?

1 Like

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

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘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/’, attribute] as String[],
FailureHandling.STOP_ON_FAILURE)

println(Strattribute)

WebUI.navigateToUrl(Strattribute)

1 Like

WebUI.verifyElementPresent(findTestObject(‘Page_Katalon Studio - Simple Power/a_Features’), 7)

what is 7 in the above command

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

Thank you for great answer!

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 :

1 Like

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.

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

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

@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.

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?

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.

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.

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())

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.