How to find elements by class using katalon studio?


#1

Hello,

using Katalon Studio, I want to find elements by class. I tried to use the following script:

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.WebDriver as WebDriver
import java.util.List
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
WebUI.openBrowser('https://sport.sky.it/calcio/mondiali/2018/06/20/portogallo-marocco-risultato-gol.html')
WebDriver driver = DriverFactory.getWebDriver()
WebElement element = driver.findElements(By.className("lb-post lb-type-text "))

but it is uncorreclty because I have some errors in console.

Can you help me, please?

Thanks


Extract number from a string
#2

I notice you have a trailing space in your call to By.className.

By.className("lb-post lb-type-text ") // <== remove the last space!

This should also work:

By.cssSelector(".lb-post.lb-type-text")

#3

Hello and thanks.

I have tried to change the class name and to find elements by css but the console returns some errors.

1. Find elements by css (Log Viewer)

Test Cases/Liveblog/New Test Case FAILED because (of) org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ‘[[[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text], [[CChromeDriver: chrome on XP (d1d2c3e65eb443976575206383193390)] -> css selector: .lb-post.lb-type-text]]’ with class ‘java.util.ArrayList’ to class ‘org.openqa.selenium.WebElement’ due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.openqa.selenium.WebElement(org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement, org.openqa.selenium.remote.RemoteWebElement)

2. Change class name (Log Viewer)

Test Cases/Liveblog/New Test Case FAILED because (of) org.openqa.selenium.InvalidSelectorException: invalid selector: Compound class names not permitted

(Session info: chrome=67.0.3396.87)

(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 0 milliseconds

For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html

Build info: version: ‘3.7.1’, revision: ‘8a0099a’, time: ‘2017-11-06T21:07:36.161Z’

System info: host: ‘DESKTOP-UN3M3AH’, ip: ‘10.25.204.62’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_102’

Driver info: com.kms.katalon.selenium.driver.CChromeDriver

Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.35.528161 (5b82f2d2aae0ca…, userDataDir: C:\Users\fabio\AppData\Loca…}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.87, webStorageEnabled: true}

Session ID: f74bafb212b9a43d740f5a8cc0bba731

*** Element info: {Using=class name, value=lb-post lb-type-text}


#4

I had some difficulties to locate element By.className so I used By.xpath instead.
Try:
By.xpath(’//*[@class=“lb-post lb-type-text”]’)


#5

Now, I have created this script:

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.WebDriver as WebDriver
import java.util.List
import java.util.ArrayList
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.remote.RemoteWebDriver
import org.openqa.selenium.remote.RemoteWebElement
WebUI.openBrowser('https://preaem.tg24-resp.sky.it/politica/2018/06/12/QAteststream_liveblog_white_label.html')
WebDriver driver = DriverFactory.getWebDriver()
ArrayList<WebElement> fields = driver.findElements(By.cssSelector(".lb-post lb-type-html"))

and the console doesn’t return any message error. Is this a correctly script?

Thanks


#6

Yes, it is. Now, you have a list of WebElements and you can do stuff with it.


#7

Oh great!!

Now, Can I verify the number of elements field? I have used:

WebUI.openBrowser('https://preaem.tg24-resp.sky.it/politica/2018/06/12/QAteststream_liveblog_white_label.html')
WebDriver driver = DriverFactory.getWebDriver()
ArrayList<WebElement> fields = driver.findElements(By.cssSelector(".lb-post lb-type-html"))
int totalelementsfind = fields.size()
System.out.println(totalelementsfind)

but it doesn’t work.

Thanks


#8

What does “doesn’t work” mean? Does it throw an exception or returns 0 as a size? In second case, doublecheck your CSS selector.


#9

This,

findElements(By.cssSelector(".lb-post lb-type-html")

is wrong. Your CSS is saying find an element with class “lb-post” that has a descendant tag called “lb-type-html” <== there is no such tag in standards compliant HTML.

In CSS, class names BEGIN WITH A DOT.

And like Marek said, be more specific about your problem.

1 - show us the code
2 - show us the error caused by your code (if any)
3 - tell us what you are trying to achieve


#10

Hi,

the issue is resolved and, after the fix, the script functions correctily.

Thanks!


#11

Can someone please tell me what mistake i made here ?

@Keyword
def expath123()
{
WebDriver webDriver = DriverFactory.getWebDriver()

	WebElement element= webDriver.findElements(By.xpath("(//input)[34]"))
	element.clear()
	element.sendKeys("new schedule meeting")
	
}

Error Log :

11-07-2019 05:47:50 PM samplescript.schedulemeeting.expath123()

Elapsed time: 1.024s

Cannot cast object ‘[[[CChromeDriver: chrome on XP (b616b1d32fb382d4c82bb44e61ab92be)] -> xpath: (//input)[34]]]’ with class ‘java.util.ArrayList’ to class ‘org.openqa.selenium.WebElement’ due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.openqa.selenium.WebElement(org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement)


#12

This is wrong:

    WebElement element= webDriver.findElements(By.xpath("(//input)[34]"))

Change it to:

    List<WebElement> elements = webDriver.findElements(By.xpath("(//input)[34]"))

Otherwise, change to:

    WebElement element= webDriver.findElement(By.xpath("(//input)[34]"))

Please find the difference between findElements and findElement : plural and singular.