如何单击表格单元格中的按钮

时间:2014-08-21 08:50:04

标签: c# selenium xpath selenium-webdriver

我正在尝试单击表格单元格中的按钮。这个单元格包含几个按钮。 表有5行和10列。按钮单元格是最后一个。 在Firefox中尝试使用xpath工具,但我得到一个例外,找不到该元素。 尝试了这些xpath变体:

Drv.Driver.FindElement(By.XPath("//table[@id='SERVICE_CANCEL']/tbody/tr[" + 4 + "]/td[10]")).Click();
Drv.Driver.FindElement(By.XPath(".//*[@id='SERVICE_CANCEL']/tr[" + 4 + "]/td[10]/div")).Click();
Drv.Driver.FindElement(By.XPath("//[@serid='10002455']/tr[4]")).Click();

这些都不起作用。同样在每一行的第10个单元格中,如前所述,还有一组按钮。没有任何其他想法如何点击它。如果你能帮助我会很感激。

因为HTML代码有点"巨大"所以它的一部分:

<TR class=datagrid-row id=datagrid-row-r2-2-3 datagrid-row-index="3">
<TD style="DISPLAY: none" field="index">
<DIV class="datagrid-cell datagrid-cell-c2-index" style="HEIGHT: auto; WHITE-SPACE: normal; TEXT-ALIGN: left">4</DIV></TD>

<TD field="name">
<DIV class="datagrid-cell datagrid-cell-c2-name">Text1</DIV></TD>
<TD field="ID">
<DIV class="datagrid-cell datagrid-cell-c2-ID">000</DIV></TD>
<TD field="ID2">
<DIV class="datagrid-cell datagrid-cell-c2-ID2">111</DIV></TD>
<TD field="ID3">
<DIV class="datagrid-cell datagrid-cell-c2-ID3"></DIV></TD>

<TD field="buttons">
<DIV class="datagrid-cell datagrid-cell-c2-buttons" style="HEIGHT: auto; WHITE-SPACE: normal; TEXT-ALIGN: left">
<DIV class=gridbuttons><A title="shutdown" class="button enabled" id=SERVICE_CANCEL serid="10002455"></A></DIV></TD></TR>

3 个答案:

答案 0 :(得分:1)

该按钮具有ID(SERVICE_CANCEL)。首先,您应该纠正HTML,因为您需要这样的引号:<a id="SERVICE_CANCEL">然后只需使用:

 Drv.Driver.FindElement(By.Id("SERVICE_CANCEL")).Click();

当您拥有ID时,您不需要任何xpath

答案 1 :(得分:0)

Shien,因为我们不能在具有ID的表格单元格值中使用ID(因为这些可以动态生成或不唯一),您需要提供整个xpath。

我已经使用了你的HTML并更新了你可以看到**的最后一个值,以下是为我工作的硒代码。

HTML代码:

<html>
<body>
<table border="1" width="200px">
<TR class=datagrid-row id=datagrid-row-r2-2-3 datagrid-row-index="3">
<TD style="DISPLAY: none" field="index">
<DIV class="datagrid-cell datagrid-cell-c2-index" style="HEIGHT: auto; WHITE-SPACE: normal; TEXT-ALIGN: left">4</DIV></TD>
<TD field="name">
<DIV class="datagrid-cell datagrid-cell-c2-name">Text1</DIV></TD>
<TD field="ID">
<DIV class="datagrid-cell datagrid-cell-c2-ID">000</DIV></TD>
<TD field="ID2">
<DIV class="datagrid-cell datagrid-cell-c2-ID2">111</DIV></TD>
<TD field="ID3">
<DIV class="datagrid-cell datagrid-cell-c2-ID3"></DIV></TD>

<TD field="buttons">
<DIV class="datagrid-cell datagrid-cell-c2-buttons" style="HEIGHT: auto; WHITE-SPACE: normal; TEXT-ALIGN: left">
<DIV class=gridbuttons>**<input type=button value="shutdown" class="button enabled" id=SERVICE_CANCEL serid="10002455" onclick="calFunc()"></input>**</DIV></TD></TR>
</table>
<script>
function calFunc()
{
  alert("Hello World");
} 
</script>
</body>
</html>

Java中的Selenium代码:

driver.findElement(By.xpath("//table/tbody/tr[1]/td[6]/div/div/input[@value='shutdown']")).click();

答案 2 :(得分:0)

挖掘后发现解决方案更多。 我使用的Xpath是:

//a[contains(@id, 'SERVICE_CANCEL') and contains (@serid, '" +tempPaslaugosIDReiksme + "')]"
相关问题