Get specific value based on column & row name from dynamic Web table in which columns & rows position also dynamic

萝らか妹 提交于 2021-01-28 20:20:18

问题


I have to print a specific value based on column and row names. Bt the problem is that columns position and rows position changes every time. And table data is dynamic, values are changing continuously. All the values are not constantmy dynamic web table is. and i want specific value of month April in year 2001.


回答1:


Here is the sample table based on the attached screenshot.

<html>
	<table id='table1' border=" 1px solid black">
	<tr>		
		<th>Month/Year</th>
		<th>2003</th>
		<th>2004</th>
		<th>2001</th>
		<th>2005</th>
		<th>2002</th>
	</tr>	
	<tr> 
		<td>May</td>
		<td>122</td>
		<td>84</td>
		<td>7777</td>
		<td>12</td>
		<td>122</td>
	</tr>

	<tr> 
		<td>Feb</td>
		<td>45</td>
		<td>445</td>
		<td>565</td>
		<td>222</td>
		<td>122</td>
	</tr>
	<tr> 
		<td>April</td>
		<td>3556</td>
		<td>212</td>
		<td>21</td>
		<td>1546</td>
		<td>855</td>
	</tr>
	</table>
	<span>-----------------------------------------------</span>
	<table id='table2' border=" 1px solid black">
	<tr>		
		<th>Month/Year</th>
		<th>2002</th>
		<th>2001</th>
		<th>2003</th>
		<th>2004</th>
		<th>2005</th>
	</tr>	
	<tr> 
		<td>April</td>
		<td>120</td>
		<td>243</td>
		<td>221</td>
		<td>21</td>
		<td>65</td>
	</tr>

	<tr> 
		<td>May</td>
		<td>96</td>
		<td>146</td>
		<td>454</td>
		<td>452</td>
		<td>4566</td>
	</tr>
	<tr> 
		<td>March</td>
		<td>788</td>
		<td>139</td>
		<td>10</td>
		<td>1001</td>
		<td>013</td>
	</tr>
	<tr> 
		<td>Feb</td>
		<td>552</td>
		<td>1245</td>
		<td>545</td>
		<td>41</td>
		<td>41</td>
	</tr>
	</table>
</html>

Below is the xpath to get the value of "April/2001" from table 1.

//table[@id='table1']//td[position()=count(//th[normalize-space(.)='Month/Year']/preceding-sibling::th)+1 and normalize-space(.)='April']/ancestor::tr//td[position()=count(//table[@id='table1']//th[normalize-space(.)='2001']/preceding-sibling::th)+1]

And xpath to get the value of "April/2001" from table 2.

//table[@id='table2']//td[position()=count(//th[normalize-space(.)='Month/Year']/preceding-sibling::th)+1 and normalize-space(.)='April']/ancestor::tr//td[position()=count(//table[@id='table2']//th[normalize-space(.)='2001']/preceding-sibling::th)+1]

The only difference between the 2 xpaths' above is table id **//table[@id='tableX**. Please update the table locator as per your application values. Let me know if this is helpful.



来源:https://stackoverflow.com/questions/54995924/get-specific-value-based-on-column-row-name-from-dynamic-web-table-in-which-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!