How to click the 2nd Element in xpath search in the Object Repository

Hi Everyone,

I have a Object Repository
which is btnAHOU_A with a value of //*[contains(@id,‘_1_1’)]

now base on google chrome, it has around 30 matches depends on the time

How to scroll into element and click for that scenario in Groovy? i want to get the 2nd / 3rd etc match

i tried calling in groovy

WebUI.click(bt_AHOU_A[2]) does not work

Hi @aaron.gargantos ,

I will try to ask internally to support, in the meanwhile, can you please help me clarify that you have such 30 similar buttons using the exactly same xpath? And all of them will appear in the page statically?

Thank you!

Hi @Elly_Tran

here is a sample from other xpath similar to it
base on my screenshot, 1 xpath can see atleast 8 similar Object

all of it will appear in 1 page.

1 want to get the 2nd or 3rd rather the 1st one that appears

i also did try this method, however if the xpath is not visible in the 1st part of the page and it will fail, i need to scroll more but function of scrollIntoElement does not work with this List Webelements

List oddsList = WebUI.findWebElements(odds, 5)
oddsList.get(1).click() //Select 1st Odds


Your XPath is too generic. You need to identify the group of buttons (perhaps via the <div> container) and then work through the button <div> elements 2..N. You might gain some benefit from using a dynamic Test Object whose selector is updated in a loop.

You only show the HTML to the div tags, so if there is much below them, you will have to flush this out.
How about trying:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import org.openqa.selenium.By as By
import org.openqa.selenium.Keys as Keys
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement

import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

List<WebElement> oddsList = DriverFactory.getWebDriver().findElements(By.xpath('//div[@class="odds_btn"]'))
WebUI.comment("Found a list of ${oddsList.size().toString()}")
for (cnt = 0; cnt < oddsList.size(); cnt++) {
    oddsList.get(cnt).click() //Select each Odds
    WebUI.delay(2)
}
3 Likes

To get the second option, your object XPATH needs to be (//*contains(@id, ‘_1_1’)])[2]

However, instead of creating a new variable for each position, I would recommend parameterizing the position, then you can just update the position any time that object is called.

For example, your XPATH would read: (//*contains(@id, ‘_1_1’)])[${position}]

image

Then, when you call this variable during a test, you can just double-click on the object column and add the position variable with the location you desire:

Thank you so much @grylion54

that one works :smiley:

1 Like

is there a way to get the value “2” from groovy script ?