How to get text from web page?

Hi There,
I need to get text from web page and then it will be stored in to excel.

I have div element likes below:

<div class="heading-middle">
       <h1 class="margin-bottom-s">Hamid, Tarmizi</h1>                      
 </div>

I want to grab “Hamid, Tarmizi”, this text is dinamyc and will be changed according who is current loging in.

And css as below:

@media (min-width: 1200px)
h1, .h1 {
     font-size: 96px;
     line-height: 84px;
}

Thanks in advance,

Tarmizi

1 Like

hi,

try this

@Timo_Kuisma1 WebUI.getAttribute() will not work here.

@tarmizi2001 You should use WebUI.getText() which will retrieve the innerText of a selector.

Based on the HTML you posted, this CSS selector will target the relevant <div> element:

div.heading-middle > h1.margin-bottom-s
1 Like

hi,

ouh, sorry wrong paste :smile:

Use GetElementText from the Element. And if you want to verify the text matches the expected result, you can add another test step - VerifyMatch

1 Like

Hi,
@Russ_Thomas
@Timo_Kuisma1

Thanks very much for response. It still doesn’t work.
Here are the actual html elements that I try to grab the text value:

<div style>
    <h2 class="p3 text-center margin-bottom-xl margin-bottom-sm-s" style="">
        WELCOME, Hamid, Tarmizi
    </h2>
</div>

And my latest script using web element:

WebDriver driver = DriverFactory.getWebDriver()
List list = driver.findElements(By.className(“p3 text-center margin-bottom-xl margin-bottom-sm-s”))
for(WebElement l : list){
print("print: "+l.getText())
}

No error but the list is empty.

Is there any way to get text using element class?

Thanks in advance,
Tarmizi

I don’t use those APIs and I’m too lazy to go look them up.

This guy on the other hand… @Brandon_Hein

My guess is that your list is empty, and it’s not a problem with getText(). Can you please print the size of your list to verify?:

WebDriver driver = DriverFactory.getWebDriver()
List list = driver.findElements(By.className(“p3 text-center margin-bottom-xl margin-bottom-sm-s”))
print(list.size())

Sorry, you’ve already stated that your list is empty… But again, this is not a problem with getText(), you are using it correctly. What you need is a proper locator to find that specific element. I generally use xpath to do this. I don’t know what the rest of your html looks like, but to get a list of <h2> elements and print them, it’s as simple as:

WebDriver driver = DriverFactory.getWebDriver()
List<WebElement> elements = driver.findElements(By.xpath("//h2"))
for(WebElement element : elements) {
    print("Heading text is: " + element.getText())
}

It worked.
Many thanks to all of you.

1 Like

Trying to do the same but not getting a response. Any thoughts…

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)

Did you get the above class description by Copy and Paste or did you type it yourself?

Also, you should create your own question on this forum rather than tag onto the tail of this one which has a Solution.

Copy paste
Dont know how to start a new question.

Then, it would seem your statement is not finding the “className”. Try to put a delay or wait in your script before this statement until the elements are visible/clickable/stable.

Will give it a shot and let you know thanks.


No such luck still returns 0 for some reason. This is kicking my butt I just did something almost the exact same like 8 lines earlier.

Sorry, you misunderstood. I meant for you to put your delay before you took your sampling of the web elements.

WebUI.delay(8)

stagerows = driver1.findElements(By.xpath("//mat-row"))
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())

or even

WebUI.navigatToUrl(DEVURL)
WebUI.waitForPageLoad(10)

WebUI.delay(1)

stagerows = driver1.findElements(By.xpath("//mat-row"))
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())
WebUI.navigateToUrl(GlobalVariable.DEVORGSETTINGS)
def x7stages= CustomKeywords.'testingKeywords.Testing_Objects.makeTO'('//div[(text() = "x7 Stages" or . = "x7 Stages")]')
WebUI.click(x7stages)
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
	enabledStageNames.add(stageName)
  }
}
WebUI.navigateToUrl(GlobalVariable.DEVURL)
WebUI.comment(enabledStageNames.size().toString())
for (String stageName in enabledStageNames){
	
	WebUI.verifyTextPresent(stageName, false)

This seemed to work I got the count I was looking for, but I am having issues with the last line(verify text but I think I can fix that one. Thank you for your help.
image

Got it figured out… Had to build it this way to get it to work.

WebUI.navigateToUrl(GlobalVariable.DEVORGSETTINGS)
def x7stages= CustomKeywords.'testingKeywords.Testing_Objects.makeTO'('//div[(text() = "x7 Stages" or . = "x7 Stages")]')
WebUI.click(x7stages)
WebDriver driver1 = DriverFactory.getWebDriver()
List<WebElement> stagerows = driver1.findElements(By.xpath("//mat-row"))
def enabledStageNames = []
for (WebElement stagerow in stagerows) {
Boolean isEnabled = stagerow.findElements(By.className("mat-checkbox-checked")).size() > 0
  if (isEnabled==true) {
	  WebElement stageDisplaynameCell = stagerow.findElement(By.xpath(".//mat-cell[contains(@class, 'cdk-column-stage-displayName')]"))
	def stageName =stageDisplaynameCell.getText()
	enabledStageNames.add(stageName)
  }
}
WebUI.navigateToUrl(GlobalVariable.DEVURL)
def zoomBtn = CustomKeywords.'testingKeywords.Testing_Objects.makeTO'('//button[(text() = "fullscreen" or . = "fullscreen")]')
WebUI.click(zoomBtn)
WebUI.comment(enabledStageNames.toString())
for (String stageName in enabledStageNames){
	
	WebUI.verifyTextPresent(stageName, false, FailureHandling.CONTINUE_ON_FAILURE)
}