【发布时间】:2016-08-20 09:09:17
【问题描述】:
我们如何使用它包含的值获取表格(在网页中)行。
假设有一个 3*3 的表格。在那个单元格(2,3)单元格文本是“测试”。那么如何使用单元格文本“Test”找到行号 = 2。
硒-2.53.1 Java-8
这是 xpath
//table[2]/tbody/tr/td[2]/table[2]/tbody/tr[7]/td/div[2]/table/tbody/tr[1]/ td[1]/div/a/font
其中粗体 tr(此处为 tr[1])是我要动态更改的行,文本位于粗体 td(此处为 td[1])内
我的html代码是:
<html>
<head></head>
<body>
<form name="fm" method="post">
<table>
<!-- --Some data--- -->
</table>
<table width="100%" height="100%" border="0" >
<tr valign="top" >
<td width="200" height="100%" rowspan="3" align="center" >
<p></p>
<table width="89%" height="270" border="0">
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<!-- --Some data--- -->
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td><table width="100%" height="71" border="0" align="right">
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</table></td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<!-- --Some data--- -->
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</table>
<p></p>
</td>
<td width="622" height="30" colspan="3" align="left" valign="top">
<table width="100%" height="40" align="center">
<tr><!-- --Some data--- --></tr>
</table>
<table width="476" height="110" align="center">
<tr></tr>
<tr></tr>
<tr></tr>
<!-- --Some data--- -->
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td height="22" colspan="3">
<div align="left">
<table width="469" border="1">
<tr bgcolor="#006291">
<td width="81" bgcolor="#006291"><div align="center">SlNo.</div></td>
<td width="82" bgcolor="#006291"><div align="center">Code</div></td>
<td width="300" bgcolor="#006291"><div align="center">Name</div></td>
</tr>
</table>
</div>
<div align="left" style="width:465px;Height:150px;">
<table width="465" bgcolor="#FFFFFF" border="1">
<tr bordercolor="#006291" bgcolor='#FFEADF' >
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >1</font></a></div></td>
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >01</font></a></div></td>
<td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >tvm1</font></a></div></td>
</tr>
<tr bordercolor="#006291" bgcolor='#F1F8FC' >
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >2</font></a></div></td>
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >02</font></a></div></td>
<td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >tvm2</font></a></div></td>
</tr>
<tr bordercolor="#006291" bgcolor='#FFEADF' >
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >3</font></a></div></td>
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >03</font></a></div></td>
<td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >tvm3</font></a></div></td>
</tr>
<tr bordercolor="#006291" bgcolor='#F1F8FC' >
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >4</font></a></div></td>
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >04</font></a></div></td>
<td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >tvm4</font></a></div></td>
</tr>
<tr bordercolor="#006291" bgcolor='#FFEADF' >
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >5</font></a></div></td>
<td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >10</font></a></div></td>
<td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >tvm5</font></a></div></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
【问题讨论】:
-
能否分享您的表格 HTML 以便更好地理解??
-
是的..我已经添加了
-
所以这里要根据文本搜索行号。对吧??
-
是的..我想在文本的基础上点击td(这里是td with href="t.php")。
-
基于文本或获取该表最后一行的索引(正在更新,即行正在动态添加到该表中)。这样我就可以点击最后一行
标签: java selenium selenium-webdriver html-table