How to make xpath calls directly from a Katalon script?

Man, i feel a bit slow even having to ask this, but I’ve spent a good amount of time hunting for it and still haven’t figured it out. What command do i use if i want to locate an object on a page by its xpath reference instead of its Object Repository path?

Like if i wanted to use WebUI.click(), what would be the xpath equivalent of findTestObject()?

Hey Jeremy

I’m not entirely sure what you’re asking. If you want to create TestObject “live” in memory (instead of building them and storing them in the Object Repo) you can use makeTO() (note: you can change to using xpath instead of css if you wish).

Also documented here:

If that’s not what you want to do, let me know.

1 Like

You wouldn’t be able to use WebUI.click() without referencing a test object, but you could always just use pure selenium to do the same thing:

WebDriver driver = DriverFactory.getWebDriver()
WebElement element = driver.findElement(By.xpath("//my//xpath"))
element.click()
3 Likes

Alternatively you can create a TestObject instance specifying a XPath expression as its property.

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
...
TestObject tObj = new TestObject()
tObj.addProperty("xpath", ConditionType.EQUALS, "//my//xpath")
WebUI.verifyElementPresent(tObj)
...
2 Likes

Thanks @Brandon_Hein, I think that gets me mostly what I was looking for.

@Russ_Thomas Basically, its for when I’m scripting by hand, and I have to check an attribute or click on a particular element that, for whatever reason, I can find pretty quickly/easily through xpath, but would be a hassle for me to track down in my object repo.

I’m just trying to be efficient with my coding practices, so that i’m as comfortable using xpath to access DOM elements as i am with pulling them into my script from the object repo.

@kazurayam, Nice i didn’t realize you could instantiate and assign objects that way, very cool. I’ll have to read up on that TestObject, that looks useful!

You’re on the right track. Several of us actually don’t even use the object repository at all :wink:

Hello @Brandon_Hein, How about set text into target Xpath without object, but use pure selenium ?

It’s the same thing, but you would use the sendKeys() method:

WebDriver driver = DriverFactory.getWebDriver()
WebElement element = driver.findElement(By.xpath("//my//xpath"))
element.sendKeys("abc")

I would recommend reviewing the Selenium API if you want to go down this route, as there are a lot of useful methods at your disposal:

https://www.javadoc.io/doc/org.seleniumhq.selenium/selenium-api/2.50.1/org/openqa/selenium/WebDriver.html

1 Like