Need help with using Xpath within the script. Object being located is within an iframe

Hello All

Need your help figuring out to write the xpath for an object located within an iframe, in a groovy script
Elements are to be extracted from a web table embedded within an iframe. screen is as below


I want to be able to get the Plan, Member, Coverage info for the two rows 01 and 02 we see on the screen(there could be more too if more plans are there)
I was trying to read all elements into an ArrayList and then was going to try writing it out to my results spreadsheet.
using the for loop so I can pass an index to identify which row I want the data from like below
for (def index = 2; index <= planCount; index++) {
plan = WebUI.getText(findTestObject(By.xpath(“//iframe[@name=‘_iframe-popupWin’]//div[@id=‘contentstart’]/div/table/tbody/tr[index]/td[2]”)))
println("Plan: ") + plan
}
The object of the webtable I am trying to get to starts with //div[@id = ‘contentstart’… which is located within an iframe, the xpath to which is the preceding xpath - right after findTestObject. What would be the proper syntax to get to the object? Will appreciate any help I can get. Thanks!

Please use Code Formatting system to present your code more readable.

Before trying to get access to the element in <iframe>, you need to switch the context to the iframe element by the following Keyword.

Thanks for your replies. Trying to go through the doc you sent.
Code I am using is as below: Syntax for xpath with a parent iframe I am not able to figure out

for (def index = 2; index <= planCount; index++) {
				plan = WebUI.getText(findTestObject(By.xpath("//div[@id='contentstart']/div/table/tbody/tr[index]/td[2]parent::div")))
			    println("Plan: ") + plan
			}

Unless you share the full HTML source code, I can not tell if your code is right or not.

At least, this code is syntactically wrong.

It’s a long piece of code: Basically I am adding plans in the UI, which look like the screen 1 I sent earlier. It’s in a web table within an iframe. I need to get elements out from there and write it to my results spreadsheet. Can we even specify an xpath in groovy?


			//SAVE Plan
			WebUI.click(findTestObject('ePost GUI/pagePlan/input_Today_SAVE'))
			WebUI.delay(1)

			//Check for Alerts in Plan fields.

			//If Alert encountered do not continue
			if (!alertCaught ) {
				if( dataValues_Plan.get( "AddPlan" ).equalsIgnoreCase( "true" ) && ++planCount < 4 ) {
					//Skip to the next row in Plan sheet, to add another plan
					rowPlan++
					dataValues_Plan = WDSpreadsheet.ReadSpreadsheetRowAsMap( "Reports/" + xlsName, "Plan", rowPlan )
					firstPlan = false
					planCount++
				}
				else {
					finishedAddingPlans = true
				}
			}
		}
	} //End of while loop for adding Plans
		
		//Write results to PLAN sheet
		if (!alertCaught) {
			//Write results to PLAN sheet
			for (def index = 2; index <= planCount; index++) {
				WebUI.switchToFrame(findTestObject('ePost GUI/pagePlan/iframe_epostRx Pop-up__iframe-popupWin'), 5)
				plan(i) = WebUI.getText(findTestObject("//div[@id='contentstart']/div/table/tbody/tr[index]/td[2]"))
			    println("Plan: ") + plan(i)
				colNumber = WDSpreadsheet.FindSpreadsheetColumn( "Reports/" + xlsName, "Plan", 1, "PLAN_Plan" )
				WDSpreadsheet.WriteToSpreadsheet( "Reports/" + xlsName, "Plan", colNumber, rowPlan--, Plan1 )
			}

htmal source code is quite big. Trying to paste here. It’s within the modal pop ups inside the iframe

NVKRC ![image|408x499](upload://eGS6n5Z29ZHsanDmDflDfjS4AbG.png)

I do not understand your question.

Is it a public URL on the Internet? Then tell it to us.

I want to specify an xpath in the script, as I am parameterising the indexes to be able iterate and read thru the web table. Hope you saw the screenshot of the web table whose elements I want to get. The number of rows it could contain will vary according to number of plans added. So I wanted to do a ‘for’ loop to iterate through the rows and get the text out of the web table elements using xpath to specify the location of the object within the web table.
so xpath somewhere in this for loop

for (def index = 2; index <= planCount; index++) {
				WebUI.switchToFrame(findTestObject('ePost GUI/pagePlan/iframe_epostRx Pop-up__iframe-popupWin'), 5)
				plan = WebUI.getText(findTestObject("//div[@id='contentstart']/div/table/tbody/tr[index]/td[2]"))
			    println("Plan: ") + plan
			}

It is not a public URL. It’s internal

You want to make a Test Object with a XPath dynamically created you test case scrit.

Then

You should not use findTestObject() any more. The findTestObject() function looks into the Test Objects folder in the project directory to pick up an entry.

Can you please give me the syntax to use locating the object by xpath?

In selenium we had the findElement(By.xpath…

Katalon has WebUI.findWebElement() and WebUI.findWebElements() keyword.

Those are very thin wrapper to the selenium’s findElement() and findElements() method.


And, you can use the Selenium API in Katalon Studio as well. An example is here:

The point you need to learn is just a single line as follows:

	WebDriver driver = DriverFactory.getWebDriver();

So I wanted to … get the text out of the web table elements using xpath to specify the location of the object within the web table.

I once created a project on Github for somebody. You have a similar problem as that. So I added a sample Test Case

You can download the zip of the project from

unzip it, open it in your KS, open the TC2, and run.

Then nobody, other than yourself, would be able to tell if a xpath expression is valid or not against your target HTML.

c/c @sara.leslie

Thanks for your help. Downloaded tc. Going through it now