Gettext from list of elements not returning the text

I am trying to loop through the list of a webelement and get the text. So I can verify the text is same as expected

in my script

By menuListObj = By.cssSelector(".navigation-wrapper .navigation-menu .navigation-menu-item a")
CustomKeywords.‘ciautils.ciaUtilities.verifyNavMenu’(driver, menuListObj, careerNavbarList)

def verifyNavMenu(WebDriver driver, By object, List expectedMenuName){
List menuNameInApp = driver.findElements(object)

tried these both options but the output is shown as below

Option1
for (String s : menuNameInApp){
println (s)
}
Option2
for (int i = 0; i <menuNameInApp.size(); i++){
String actualMenuName = menuNameInApp.get(i).getText()
println(i+1 +". menu name "+actualMenuName )
If (actualMenuName == expectedMenuName(i)){
println(“Sucess”)
}
else{
println(“Failed”)
}

}
}

Output

  1. menu name [[CChromeDriver: chrome on WINDOWS (23024dcbcb923168d4e308da435981ad)] → css selector: .navigation-wrapper .navigation-menu .navigation-menu-item a]
  2. menu name [[CChromeDriver: chrome on WINDOWS (23024dcbcb923168d4e308da435981ad)] → css selector: .navigation-wrapper .navigation-menu .navigation-menu-item a]
  3. menu name [[CChromeDriver: chrome on WINDOWS (23024dcbcb923168d4e308da435981ad)] → css selector: .navigation-wrapper .navigation-menu .navigation-menu-item a]

Not sure why its printing the selector and not the text

I am wondering why the parentheses } is missing, which closes the definition of function verifyNavMenu. If } is there or not — it is semantically very important. Is } really missing? or carelessly omitted in that quotation only?

Not sure, but this part is definitely not going to work:

for (String s : menuNameInApp){
println (s)
}

Since menuNameInApp is a list of WebElements, it will never be a String :wink: Try something like

for (WebElement e: menuNameInApp {
println e.getText()
}

and let us know the results.

I tried the above change and still not working. Not sure why a simple line of selenium script not working in katalon studio.
All I am trying to do is get the list of elements and gettext from each element in the array. This works when I try on selenium. But in this tool its printing the locator instead of text. Am I missing anything here

This is the html I have

<div class="navigation-wrapper ">
    <nav aria-label="Primary Navigation">
        <ul class="navigation-menu">
           <li class="navigation-menu-item">
              <a href="/careers/test1/">test1</a></li>
           <li class="navigation-menu-item">
              <a href="/careers/test2/">test2</a></li>
           <li class="navigation-menu-item">
              <a href="/careers/test3/">test3</a>
            </li></ul></nav></div>

My code I tried is

 @Keyword
 	  def verifyNavMenu(WebDriver driver, By object, List<String> expectedMenuName){
     		WebElement menuObj = driver.findElement(object)
     		List<WebElement> menuNameInApp = menuObj.findElements(By.xpath("//li[contains(@class, 'navigation-menu-item')] /a"))
     		for (WebElement s : menuNameInApp){
     			System.out.println (s.getText())
     		}
       for (int i = 0; i <menuNameInApp.size(); i++)
     	  {
     	    String actualMenuName = menuNameInApp.get(i).getText()
     	    println(i+1 +". menu name "+actualMenuName)
         }
     }

In my main script

By menuListObj = By.xpath("//div[contains(@class, ‘navigation-wrapper’)]")

CustomKeywords.‘ciautils.ciaUtilities.verifyNavMenu’(driver, menuListObj, careerNavbarList)

The output it is printing is

  1. menu name
  2. menu name
  3. menu name
  4. menu name
  5. menu name

If I do getAttribute then its working and printing the value

Finally got the issue fixed, this is all because of not waiting for the page to load. after trying a wait statement then it worked. Thanks for everyones support though

1 Like

how do i write the webdriver locator in WebUI syntax.
WebUI.waitForElementClickable(By.cssSelector(“div.utility-navigation”), 10)

You’d need to pass a Test Object to WebUI keywords. You can create a temporary Test Object like this:

		TestObject to = new TestObject()
		to.addProperty("CSS", ConditionType.EQUALS , selectorValue)

and use that

WebUI.waitForElementClickable(to, 10);

I am getting following error
11: to.addProperty(“CSS”, EQUALS, “div .utility-navigation”)
2020-01-29 11:35:30.221 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/Career_Landingpage FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: ConditionType for class: Script1580161905483

@vtanguturi

Try Ctrl + Shift + O to auto import the necessary libraries.

1 Like

Hi @ThanhTo, it seems like adding a property of css type is not working, I am getting following error

11: obj.addProperty(“CSS”, EQUALS, “div .utility-navigation”)
12: waitForElementClickable(obj, 10)
Unable to find the element located by ‘By.xpath: //[@CSS = ‘div .utility-navigation’]’. Please recheck the objects properties to make sure the desired element is located.
2020-01-29 12:18:53.996 WARN k.c.w.k.b.WaitForElementClickableKeyword - Web element with id: ‘’ located by 'By.xpath: //
[@CSS = ‘div .utility-navigation’]’ not found

But if I try xpath then its working. Just want to let you know for now its fine for me. If I need to use it in future with css will there be any thing I can do

1 Like

Ah my mistake, that piece of code adds a property to Test Object, so css property will not be read and interpreted by Katalon, but xpath property will. If you want to properly set a CSS selector then there are other APIs on Test Object too.