我正在尝试针对以下情况用C#编写硒测试。
我需要能够根据其传输类型在下表中的某些记录上单击“撤消”按钮。
但是撤消按钮由以下HTML组成
<a href="" style="vertical-align: middle" class="btn btn-primary btn-sm" ng-show="true" ng-click="executeUndoCommand(transfer)">Undo</a>
我可以选择多个撤消按钮,但是我希望能够根据传输类型选择特定的撤消按钮。
我可以在表中选择我要单击undo的行,但无法在特定按钮上找到定位符。我无法通过按钮在屏幕上的位置找到按钮,因为任何新记录都经常添加到表格顶部
第一行的HTML如下
<tr ng-repeat="transfer in completedTransfersSummary track by $id(transfer)">
<td style="vertical-align: middle" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}" ng-class="
{'highlight' : transfer.transferorBusinessId == CisBusinessID }"
class="highlight">608445</td>
<td style="vertical-align: middle" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}" ng-class="
{'highlight' : transfer.transfereeBusinessId == CisBusinessID
}">608446</td>
<td style="vertical-align: middle" nowrap="" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}">Sale or
Gift</td>
<td style="vertical-align: middle" nowrap="" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}">05/07/2018</td>
<td style="vertical-align: middle" nowrap="" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}">15/05/2017</td>
<td style="vertical-align: middle" nowrap="" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}">1.00 entitlements @
€229.27</td>
<td style="vertical-align: middle" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}">526461</td>
<td style="vertical-align: middle" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}">2017</td>
<td style="vertical-align: middle" ng-
style="transfer.isDisabled && {color:'#A6A6A6'}"></td>
<td style="vertical-align: middle;" ng-
show="undoWithdrawRoleAssignedToUser">
<div ng-show="transfer.transferorBusinessId ==
CisBusinessID">
<a href="" style="vertical-align: middle"
class="btn btn-primary btn-sm" ng-show="true" ng-
click="executeUndoCommand(transfer)">Undo</a>
<a href="" style="vertical-align: middle"
class="btn btn-primary btn-sm ng-hide" ng-show="false" ng-
click="showUndoModal(transfer)">Undo</a>
<a href="" style="vertical-align: middle"
class="btn btn-default btn-sm" ng-show="true" ng-
click="executeWithdrawCommand(transfer)">Withdraw</a>
<a href="" style="vertical-align: middle"
class="btn btn-default btn-sm ng-hide" ng-show="false" ng-
click="showWithdrawModal(transfer)">Withdraw</a>
<label style="vertical-align: middle" ng-
show="false" ng-style="transfer.isDisabled && {color:'#A6A6A6'}"
class="ng-hide"></label>
<label style="vertical-align: middle" ng-
show="false" ng-style="transfer.isDisabled && {color:'#A6A6A6'}"
class="ng-hide"></label>
</div>
</td>
</tr>
答案 0 :(得分:1)
尝试在XPath下方按TransferType值匹配“撤消”按钮:
//tr[td="Sale or Gift"]//a[@ng-click="executeUndoCommand(transfer)"]
只需将td="Sale or Gift"
替换为td="Lease"
,td="Merger"
...
答案 1 :(得分:1)
您可以尝试下一个:
private By allRowElement = By.xpath("//xpath_for_all_table_row");
private By transferType = By.xpath(".//xpath_for_transfer_type");// here "." on start is very important
private By undoButton = By.xpath(".//xpath_for_undo_button"); // here "." on start is very important
public void clickUndoBtn(String transferTypeValue) {
List<WebElement> tempList = driver.findElements(allRowElement);
for (WebElement row : tempList) {
if (transferTypeValue.equals(row.findElement(transferType).getText())) {
row.findElement(undoButton).click();
}
}
}
//return List of elements depends on transfer type value
public List<WebElement> getElements(String transferTypeValue) {
List<WebElement> tempList = driver.findElements(allRowElement);
return tempList.stream()
.filter(row -> transferTypeValue.equals(row.findElement(transferType).getText())).collect(Collectors.toList());
}
注意: WebElement 对象可以搜索元素。如果xpath以“。”开头。 WebElement对象将在自己内部搜索元素(子元素)
答案 2 :(得分:1)
根据您的问题和您提供的 HTML ,您可能需要针对文本中的文本为撤消的多个元素调用click()
传输类型列,例如销售或礼物,租赁或合并。因此,您可以编写如下函数:
public void clickUndo(string transferType)
{
driver.FindElement(By.XPath("//td[text()='" + state + "']//following::div[1]//a[@class='btn btn-primary btn-sm' and @ng-show='true']")).Click();
}
现在,您可以使用任何 Transfer Type 调用函数clickUndo()
,如下所示:
clickUndo("Sale or Gift")
clickUndo("Lease")
clickUndo("Merger")
注意:正如您提到的这是一个Angular表一样,您必须在调用click()
之前引入 WebDriverWait 。>