Katalon get row from Grid

I’m erroring on WebElement Table = driver.findElement(By.xpath(‘/html/body/div/div[4]/div/div’))

  • Error message I’m getting: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“xpath”,“selector”:“/html/body/div/div[4]/div/div”}

I’m new to Katalon and I’m trying to implement getting information from a grid, but I’m having a difficult time with the line above.

Code:
//expected value from column
String ExpectedValue = ‘03’

WebDriver driver = DriverFactory.getWebDriver()

//WebUI.waitForElementClickable(findTestObject(‘WPE2/input_.0000_ctl00ContentPlaceHolderWPERadGr_21b7ef’), 0)
//Locate EE ID

//WebDriverWait wait = new WebDriverWait(driver, 30)
//WebElement Table = wait.until(ExpectedConditions.elementToBeClickable(By.className(‘rgDataDiv’)))
//Table.click()
WebElement Table = driver.findElement(By.xpath(‘/html/body/div/div[4]/div/div’))

List Rows = Table.findElements(By.tagName(‘tr’))

println('No. of rows: ’ + Rows.size())

table: for (int i = 0; i < Rows.size(); i++) {
List Cols = Rows.get(i).findElements(By.tagName(‘td’))

for (int j = 0; j < Cols.size(); j++) {
    if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
        //Cols.get(4).findElement(By.xpath('//*[@id="ctl00_ContentPlaceHolderWPE_RadGridWPE_ctl00__0"]/td[14]')).click()

        break
    }
}

}

The highlighted tag is the tag that I am referencing for the xpath that is resulting in an error.

I think that this XPath expression is not pointing the <table> tag.

/html/body/div/div[4]/div/div/div/table

or

//div[@id="EmpInfo_wrapper"]/div/table

or more simply

//table[@id="EmpInfo"]

Or CSS Selector is even simpler:

WebElement Table = driver.findElement(By.cssSelector(’#EmpInfo’))

You have the example from the tutorial but you are not quite on target.

You want to get the “table” of elements and those are either in the header section (the thead tag) if you need to ensure their match, or in the body section (the tbody tag), if you want to ensure a match of the table data.

So, with that:

// get down to the body of the table
WebElement Table = driver.findElement(By.xpath('//table[@id="EmpInfo"]/tbody'))

// within the body of the table, get the rows
List<WebElement> Rows = Table.findElements(By.tagName(‘tr’))

println('No. of rows: ' + Rows.size())
table: 
for (int i = 0; i < Rows.size(); i++) {
     List<WebElement> Cols = Rows.get(i).findElements(By.tagName(‘td’))
 
    for (int j = 0; j < Cols.size(); j++) {
         if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {

            break table;
        }
   }
}

Also note the break statement change that I made. You want to break out of both inner and outer loop; break alone will only jump out of the inner loop, but break table will jump out of both.

@vbyrd

Do you want to grasp a <td>03</td> tag as an instance of WebElement?

Then you do not need to write nested for () {} loops yourself. A short XPath expression will do the same for you.

String ExpectedValue = '03'
WebDriver driver = DriverFactory.getWebDriver()

// get down to the body of the table
WebElement tbody = driver.findElement(By.xpath('//table[@id="EmpInfo"]/tbody'))

// find a <td>03</td> element under <tbody>
String expr = "/tr/td[ contains(text(),'${ExpectedValue}') ][1]"
WebElement td03 = tbody.findElement(By.xpath(expr))

If you want to study XPath more, read this.

I actually think I may have found out what maybe causing this issue. I’ve looked through the html and noticed that the table I’m trying to reference is in an iframe. However, I’m not stuck on identifying the iframe.
Error message:
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“xpath”,“selector”:“//*[@id=“224”]/iframe”}

Here is an image of the html and the iframe i’m referencing is ifram class = 'BBjHTMLView-content"

My code that I’ve updated to that still causes an error:
WebDriverWait wait = new WebDriverWait(driver, 10)
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(‘//*[@id=“237”]/iframe’)))

WebElement Table = driver.findElement(By.xpath(‘//table[@id=“EmpInfo”]/tbody’))

// within the body of the table, get the rows
List Rows = Table.findElements(By.tagName(‘tr’))

println('No. of rows: ’ + Rows.size())
table:
for (int i = 0; i < Rows.size(); i++) {
List Cols = Rows.get(i).findElements(By.tagName(‘td’))

for (int j = 0; j < Cols.size(); j++) {
     if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {

        break table;
    }

}
}

Thank you for your response.
I’ve tried the changes you’ve provided and I’m still getting the same error.

I’ve also tried putting the full xpath ‘/html/body/div/div[4]/div/div/div/table/tbody’, but the result is the exact same.
I’m very lost with why Katalon is having issues identifying the element I’ve specified.
image

Thank you for your response. I’ve tried all 4 of the options provided, but I’m still getting the following error:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“xpath”,“selector”:“//table[@id=“EmpInfo”]”}

Your target HTML contains <iframe>. It makes things complexed. But you have to learn how to overcome it by learning WebUI.switchToFrame.

Please search this Forum with keyword iframe. Then you will find many related post. Read them through.