How to get number of rows from table?

Hello to all,

May be someone could help me with this:

I want to get the number of rows of the nested table from a simple HTML table in code shown below.

When I run it locally in my machine save it the code as file.html or in this link Edit fiddle - JSFiddle - Code Playground and I run the following Javascript code on Chrome console, I get correctly the number of rows (3 in this case).

function getElementByXpath(path) {
   return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; 
}
getElementByXpath("//*[@id='main']/table[2]/tbody").rows.length;

Now I go to Kantu and running the same javascript code like this:

executeScript_Sandbox | function getElementByXpath(path) { ...} var r=getElementByXpath("//*[@id='main']/table[2]/tbody").rows.length; return r; | x
echo                  |	${x}	|   

I get this error:

[error] Error in executeScript_Sandbox code: Cannot read property 'rows' of null

What is missing in my code?

The HTML code is:

	<table border = 1 bgcolor="#FFE83A">
		<tbody>
			<tr>
				<td id = "main">
					<table id = "table_x">...</table>
					<div>...</div>
					<iframe src="javascript:false;" id="iframe_x" name="iframe_x" style="display:none" height="0" width="0" filterlink="?"></iframe> 
					<table class="table2_class" summary="MySummary" border = 1 bgcolor="#38FF66">
						<thead>...</thead>
						<tbody>
							<tr class="a1" >
								<td class="td_class">1</td>
								<td class="td_class">N</td>
								<td class="td_class_1">
									<div dir="" class="zzz">
										<div class="div_class">
											<a href="...">Some text</a>
										</div>
									</div>
								</td>
							</tr>
							<tr class="b1" >
								<td class="td_class">4</td>
								<td class="td_class">W</td>
								<td class="td_class_1">
									<div dir="" class="zzz">
										<div class="div_class">
											<a href="...">Some text</a>
										</div>
									</div>
								</td>
							</tr>   
							<tr class="a1" >
								<td class="td_class">7</td>
								<td class="td_class">R</td>
								<td class="td_class_1">
									<div dir="" class="zzz">
										<div class="div_class">
											<a href="...">Some text</a>
										</div>
									</div>
								</td>
							</tr>       
					</tbody>
				</table>
				</td>
			</tr>
		</tbody>            
	</table>

Thanks in advance.

I see you are using executeScript_Sandbox which has no access to the web page. You need to use executeScript, then it should work.

See also executeScript, execute script, Arrays - Selenium IDE Commands Tutorial

Hi Timo,

Thanks so much for your answer. Changing to executeScript it works.

In practice I learned about that “little detail” about of executeScript_Sandbox doesn’t have access to the web page hehe.