Zepri Hasiholan Togatorop
Katalon Apprentice
08/09/2018

Get Text from Cell on Table

I can't get text from cell on table.
I've use getText() and getAttribute('value')

example:
String celltext = Columns_row.get(column).getAttribute('value')
or
String celltext = Columns_row.get(column).getText()

but, still return 'null'
Upvote
Quote

Comments

  • Andrej Podhajský
    Katalon Expert
    08/09/2018
    ok,
    1. i guess you are using something similar to : https://medium.com/@katalon_studio/how-to-handle-web-tables-866c7c82d615
    2. do you have any errors?
    3. post HTML of table you want process (or a snipplet of that table) so we can see structure
    Upvote
    Quote
  • Zepri Hasiholan Togatorop
    Katalon Apprentice
    08/10/2018
    I've use this code.
    but this code return ''
    Upvote
    Quote
  • Andrej Podhajský
    Katalon Expert
    08/10/2018
    edited August 10
    what is result of:
    println Columns_row.size()

    since i dont see all script you are using, it's hard to tell what's wrong

    it seems that definition by which you want to get rows/columns does not return what you need

    Upvote
    Quote
  • Zepri Hasiholan Togatorop
    Katalon Apprentice
    08/10/2018
    edited August 10
    I don't see this script helping                        
    Upvote
    Quote
  • Zepri Hasiholan Togatorop
    Katalon Apprentice
    08/10/2018
    This is code program.
    return number and column is Valid.
    but when getText, return ''

    WebDriver driver = DriverFactory.getWebDriver()

    String ExpectedValue = '11/30/2017'

    int count = 0

    WebElement Table = driver.findElement(By.xpath('//*[@id="dgPaymentScheduleDetail"]/div/div[6]'))

    List<WebElement> rows_table = Table.findElements(By.tagName('tr'))

    int rows_count = rows_table.size()

    WebUI.verifyEqual(rows_count, 13, FailureHandling.STOP_ON_FAILURE)

    Loop: for (int row = 1; row < rows_count; row++) {
        List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('td'))

        int columns_count = Columns_row.size()
    println((('Number of cells In Row ' + row) + ' are ') + columns_count)

        WebUI.verifyEqual(columns_count, 8, FailureHandling.STOP_ON_FAILURE)

        for (int column = 0; column < columns_count; column++) {
            count++

            String celltext = Columns_row.get(column).getText()

            WebUI.verifyMatch(celltext, ExpectedValue, false, FailureHandling.OPTIONAL)

            if (celltext == ExpectedValue) {
                break Loop;
            }
        }
    }
    Upvote
    Quote
  • Andrej Podhajský
    Katalon Expert
    08/10/2018
    why are you starting on 2nd row?

    btw i did small test:
    html used:
    <html>
      <head>
      </head>
      <body>
    
        <div>Hello World!</div>
    		<table>
    			<tr>
    				<td>
    					<div _ngcontent5>
    						<!---->
    						<div _ngcontent5>
    							<p _ngcontent5>Some text2</p>
    						</div>
    						<!---->
    					</div>
    				</td>
    			</tr>
    		</table>
      </body>
    </html>
    script used:
    import org.openqa.selenium.By
    import org.openqa.selenium.WebDriver
    import org.openqa.selenium.WebElement
    
    import com.kms.katalon.core.model.FailureHandling as FailureHandling
    import com.kms.katalon.core.webui.driver.DriverFactory
    import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
    
    WebUI.openBrowser('c:\\\\Users\\\\somewhereOnMyDisk\\\\html\\\\hello.html')
    
    WebDriver driver = DriverFactory.getWebDriver()
    
    WebElement Table = driver.findElement(By.xpath('//table'))
    
    List<WebElement> rows_table = Table.findElements(By.tagName('tr'))
    
    def rows_count = rows_table.size()
    
    def count = 0
    
    println rows_count
    
    Loop:for (int row = 0; row < rows_count; row++) {
    	List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName('td'))
    
    	int columns_count = Columns_row.size()
    	println 'Number of cells In Row ' + row + ' are ' + columns_count
    
    	for (int column = 0; column < columns_count; column++) {
    		count++
    
    		def celltext = Columns_row.get(column).getText()
    		println celltext 
    	}
    }
    and result in console:
    08-10-2018 09:45:15 AM - [END]    - End action : openBrowser
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - driver = com.kms.katalon.core.webui.driver.DriverFactory.getWebDriver()
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - driver = com.kms.katalon.core.webui.driver.DriverFactory.getWebDriver()
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - Table = driver.findElement(org.openqa.selenium.By.xpath("//table"))
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - Table = driver.findElement(org.openqa.selenium.By.xpath("//table"))
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - rows_table = Table.findElements(org.openqa.selenium.By.tagName("tr"))
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - rows_table = Table.findElements(org.openqa.selenium.By.tagName("tr"))
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - rows_count = rows_table.size()
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - rows_count = rows_table.size()
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - count = 0
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - count = 0
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - println(rows_count)
    1
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - println(rows_count)
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - For ([row = 0, row < rows_count, (row++)])
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - Columns_row = rows_table.get(row).findElements(org.openqa.selenium.By.tagName("td"))
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - Columns_row = rows_table.get(row).findElements(org.openqa.selenium.By.tagName("td"))
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - columns_count = Columns_row.size()
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - columns_count = Columns_row.size()
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - println("Number of cells In Row " + row + " are " + columns_count)
    Number of cells In Row 0 are 1
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - println("Number of cells In Row " + row + " are " + columns_count)
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - For ([column = 0, column < columns_count, (column++)])
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - (count++)
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - (count++)
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - celltext = Columns_row.get(column).getText()
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - celltext = Columns_row.get(column).getText()
    08-10-2018 09:45:15 AM - [START]  - Start action : Statement - println(celltext)
    Some text2
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - println(celltext)
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - For ([column = 0, column < columns_count, (column++)])
    08-10-2018 09:45:15 AM - [END]    - End action : Statement - For ([row = 0, row < rows_count, (row++)])
    08-10-2018 09:45:15 AM - [PASSED] - Test Cases/__Sandbox/New Test Case - TableTest
    08-10-2018 09:45:15 AM - [END]    - End Test Case : Test Cases/__Sandbox/New Test Case - TableTes
    i cannot see error in your code ...

    Upvote
    Quote
  • Zepri Hasiholan Togatorop
    Katalon Apprentice
    08/15/2018
    Ok, thank you Andrej Podhajský
    Upvote
    Quote
Sign In or Register to comment.