首页 > 解决方案 > 如何通过selenium Webdriver根据excel中给出的ID单击删除图标

问题描述

我有一个包含用户详细信息的表格的页面,我想在从 excel.Selenium Webdriver 检索 ID 时单击删除图标,而 java 用于执行相同操作。我正在使用此代码从 excel 中检索名称:-

data.getPatientID().get(rowCnt);

表格的 HTML 代码:-

<tr class="rgRow" id="ctl00_ContentPlaceHolder1_GridUserControl1_RadGrid1_ctl00__0">
<td>PAT 033</td>
<td><a id="ctl00_ContentPlaceHolder1_GridUserControl1_RadGrid1_ctl00_ctl04_lbtnLastname" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridUserControl1$RadGrid1$ctl00$ctl04$lbtnLastname','')">Bee</a></td>
<td>&nbsp;</td>
<td>Barry</td>
<td>(833)833-8338</td>
<td>Frisco</td>
<td>physician new </td>
<td><a onclick="if(!confirm('Do you want to delete this record'))return false;" id="ctl00_ContentPlaceHolder1_GridUserControl1_RadGrid1_ctl00_ctl04_Remove" class="closeBtn" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridUserControl1$RadGrid1$ctl00$ctl04$Remove','')"></a></td>

任何帮助将不胜感激。在此处输入图像描述

**** 中的文本是名称和删除按钮。

标签: javaseleniumselenium-webdriverxpathwebdriver

解决方案


根据您共享的HTML,单击删除图标,您可以创建一个函数,该函数将参数作为名称,例如Albert,然后单击相应的删除图标,如下所示:

public void clickDeleteIcon(String fname)
{
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//tr[@class='rgRow'][contains(@id,'_ContentPlaceHolder1_GridUserControl1_RadGrid1_')]//following::td[2]/a[.='" + fname + "']//following::a[1]"))).click();
}

现在单击删除图标,您可以调用clickDeleteIcon(String fname)传递任何名称的函数,如下所示:

clickDeleteIcon("Albert");
//or
clickDeleteIcon("Bee")

推荐阅读