单击特定行中的按钮

时间:2018-03-12 13:13:41

标签: c# selenium selenium-webdriver

我目前有一个测试,在下表中添加一个条目,生成随机位置名称和值。我现在正试图点击表格中的编辑图标。

Table

我可以使用 -

找到正确的位置名称单元格
string editButton = "//td[text(),'"+locationName+ "'];
driver.FindElement(By.XPath(editButton)).Click();

但不确定如何点击此处的编辑按钮?

按钮的HTML是 -

<tr id="officeRechargeGrid_DXDataRow1" class="dxgvDataRow_Office2010Silver" style="">
    <td class="dxgvCommandColumn_Office2010Silver dxgv" align="center"><a class="dxbButton_Office2010Silver dxgvCommandColumnItem_Office2010Silver dxgv__cci dxbButtonSys" data-args="[['StartEdit',1],1]" id="officeRechargeGrid_DXCBtn3" href="javascript:;" style="font-size: 0pt;"><img title="Edit" class="dx-vam" src="/Images/editgray.png" alt="Edit" style="height:16px;width:16px;" id="officeRechargeGrid_DXCBtn3Img"></a><script id="dxss_1145064087" type="text/javascript" data-executed="true">

表格的HTML是 -                                                                      

//-->
</script></td>
            </tr>
        </tbody></table></td><td id="officeRechargeGrid_col2" class="dxgvHeader_Office2010Silver" onmousedown="ASPx.GHeaderMouseDown('officeRechargeGrid', this, event);" style="border-top-width:0px;border-left-width:0px;"><table style="width:100%;border-collapse:collapse;" cellspacing="0" cellpadding="0">
            <tbody><tr>
                <td>Location</td><td style="width:1px;text-align:right;"><span class="dx-vam">&nbsp;</span><img class="dxGridView_gvHeaderFilter_Office2010Silver dxgv__hfb dx-vam" src="/DXR.axd?r=1_37-Z658g" alt="[Filter]" style="cursor:default;"></td>
            </tr>
        </tbody></table></td><td id="officeRechargeGrid_col3" class="dxgvHeader_Office2010Silver" onmousedown="ASPx.GHeaderMouseDown('officeRechargeGrid', this, event);" style="border-top-width:0px;border-left-width:0px;border-right-width:0px;"><table style="width:100%;border-collapse:collapse;" cellspacing="0" cellpadding="0">
            <tbody><tr>
                <td>Assumed Volume</td><td style="width:1px;text-align:right;"><span class="dx-vam">&nbsp;</span><img class="dxGridView_gvHeaderFilter_Office2010Silver dxgv__hfb dx-vam" src="/DXR.axd?r=1_37-Z658g" alt="[Filter]" style="cursor:default;"></td>
            </tr>
        </tbody></table></td>
    </tr><tr id="officeRechargeGrid_DXFilterRow" class="dxgvFilterRow_Office2010Silver">
        <td class="dxgvCommandColumn_Office2010Silver dxgv" align="center">&nbsp;</td><td class="dxgv"><table class="dxeTextBoxSys dxeTextBox_Office2010Silver dxeTextBoxDefaultWidthSys" id="officeRechargeGrid_DXFREditorcol2" style="width:100%;border-collapse:collapse;" cellspacing="0" cellpadding="0">
            <tbody><tr>
                <td class="dxic" style="width:100%;"><input class="dxeEditArea_Office2010Silver dxeEditAreaSys" id="officeRechargeGrid_DXFREditorcol2_I" name="officeRechargeGrid$DXFREditorcol2" onfocus="ASPx.EGotFocus('officeRechargeGrid_DXFREditorcol2')" onblur="ASPx.ELostFocus('officeRechargeGrid_DXFREditorcol2')" onchange="ASPx.EValueChanged('officeRechargeGrid_DXFREditorcol2')" type="text"></td>
            </tr>
        </tbody></table><script id="dxss_334903315" type="text/javascript" data-executed="true">
<!--
ASPx.AddDisabledItems('officeRechargeGrid_DXFREditorcol2',[[['dxeDisabled_Office2010Silver'],[''],['','I']]]);

var dxo = new ASPxClientTextBox('officeRechargeGrid_DXFREditorcol2');
dxo.InitGlobalVariable('officeRechargeGrid_DXFREditorcol2');
dxo.uniqueID = 'officeRechargeGrid$DXFREditorcol2';
dxo.ValueChanged.AddHandler(function(s, event) { ASPx.GVFilterChanged('officeRechargeGrid',s); });
dxo.KeyDown.AddHandler(function(s, event) { ASPx.GVFilterKeyPress('officeRechargeGrid',s,event); });
dxo.RequireStyleDecoration();
dxo.styleDecoration.AddStyle('F','dxeFocused_Office2010Silver','');
dxo.AfterCreate();

//-->
</script></td><td class="dxgv" style="text-align:Right;border-right-width:0px;"><table class="dxeTextBoxSys dxeTextBox_Office2010Silver dxeTextBoxDefaultWidthSys" id="officeRechargeGrid_DXFREditorcol3" style="width:100%;border-collapse:collapse;" cellspacing="0" cellpadding="0">
            <tbody><tr>
                <td class="dxic" style="width:100%;"><input class="dxeEditArea_Office2010Silver dxeEditAreaSys" id="officeRechargeGrid_DXFREditorcol3_I" name="officeRechargeGrid$DXFREditorcol3" onfocus="ASPx.EGotFocus('officeRechargeGrid_DXFREditorcol3')" onblur="ASPx.ELostFocus('officeRechargeGrid_DXFREditorcol3')" onchange="ASPx.EValueChanged('officeRechargeGrid_DXFREditorcol3')" type="text"></td>
            </tr>
        </tbody></table><script id="dxss_1037231823" type="text/javascript" data-executed="true">
<!--
ASPx.AddDisabledItems('officeRechargeGrid_DXFREditorcol3',[[['dxeDisabled_Office2010Silver'],[''],['','I']]]);

var dxo = new ASPxClientTextBox('officeRechargeGrid_DXFREditorcol3');
dxo.InitGlobalVariable('officeRechargeGrid_DXFREditorcol3');
dxo.uniqueID = 'officeRechargeGrid$DXFREditorcol3';
dxo.ValueChanged.AddHandler(function(s, event) { ASPx.GVFilterChanged('officeRechargeGrid',s); });
dxo.KeyDown.AddHandler(function(s, event) { ASPx.GVFilterKeyPress('officeRechargeGrid',s,event); });
dxo.RequireStyleDecoration();
dxo.styleDecoration.AddStyle('F','dxeFocused_Office2010Silver','');
dxo.AfterCreate();

//-->
</script></td>
    </tr><tr id="officeRechargeGrid_DXDataRow0" class="dxgvDataRow_Office2010Silver" style="">
        <td class="dxgvCommandColumn_Office2010Silver dxgv" align="center"><a class="dxbButton_Office2010Silver dxgvCommandColumnItem_Office2010Silver dxgv__cci dxbButtonSys" data-args="[['StartEdit',0],1]" id="officeRechargeGrid_DXCBtn1" href="javascript:;" style="font-size: 0pt;"><img title="Edit" class="dx-vam" src="/Images/editgray.png" alt="Edit" style="height:16px;width:16px;" id="officeRechargeGrid_DXCBtn1Img"></a><script id="dxss_1182543207" type="text/javascript" data-executed="true">
<!--
ASPx.AddDisabledItems('officeRechargeGrid_DXCBtn1',[[['dxbDisabled_Office2010Silver'],[''],[''],['','TC']]]);

//-->
</script><a class="dxbButton_Office2010Silver dxgvCommandColumnItem_Office2010Silver dxgv__cci dxbButtonSys" data-args="[['Delete',0],1]" id="officeRechargeGrid_DXCBtn2" href="javascript:;" style="font-size: 0pt;"><img title="Delete" class="dx-vam" src="/Images/delete.png" alt="Delete" style="height:16px;width:16px;" id="officeRechargeGrid_DXCBtn2Img"></a><script id="dxss_2131039704" type="text/javascript" data-executed="true">
<!--
ASPx.AddDisabledItems('officeRechargeGrid_DXCBtn2',[[['dxbDisabled_Office2010Silver'],[''],[''],['','TC']]]);

//-->
</script></td><td class="dxgv">TAHPOYOWAG</td><td class="dxgv" style="border-right-width:0px;" align="right">49,554.00</td>
    </tr><tr id="officeRechargeGrid_DXDataRow1" class="dxgvDataRow_Office2010Silver dxgvLVR" style="">
        <td class="dxgvCommandColumn_Office2010Silver dxgv" style="border-bottom-width:0px;" align="center"><a class="dxbButton_Office2010Silver dxgvCommandColumnItem_Office2010Silver dxgv__cci dxbButtonSys" data-args="[['StartEdit',1],1]" id="officeRechargeGrid_DXCBtn3" href="javascript:;" style="font-size: 0pt;"><img title="Edit" class="dx-vam" src="/Images/editgray.png" alt="Edit" style="height:16px;width:16px;" id="officeRechargeGrid_DXCBtn3Img"></a><script id="dxss_1145064087" type="text/javascript" data-executed="true">
<!--
ASPx.AddDisabledItems('officeRechargeGrid_DXCBtn3',[[['dxbDisabled_Office2010Silver'],[''],[''],['','TC']]]);

//-->
</script><a class="dxbButton_Office2010Silver dxgvCommandColumnItem_Office2010Silver dxgv__cci dxbButtonSys" data-args="[['Delete',1],1]" id="officeRechargeGrid_DXCBtn4" href="javascript:;" style="font-size: 0pt;"><img title="Delete" class="dx-vam" src="/Images/delete.png" alt="Delete" style="height:16px;width:16px;" id="officeRechargeGrid_DXCBtn4Img"></a><script id="dxss_511888854" type="text/javascript" data-executed="true">
<!--
ASPx.AddDisabledItems('officeRechargeGrid_DXCBtn4',[[['dxbDisabled_Office2010Silver'],[''],[''],['','TC']]]);

//-->
</script></td><td class="dxgv" style="border-bottom-width:0px;">JKFZICCPJA</td><td class="dxgv" style="border-right-width:0px;border-bottom-width:0px;" align="right">76,646.00</td>
    </tr>
</tbody></table>

4 个答案:

答案 0 :(得分:3)

您尚未向我们提供主表中的ID(以缩小HTML范围),因此只需搜索页面上的所有tr

然后,从表中获取所有行。

var rows = driver.FindElements(By.TagName("tr"));

在该集合中,您将搜索所有列,并使用locationName将结果范围缩小到特定行。

var desiredRow = rows.Where(row => row.FindElements(By.TagName("td"))[1] == locationName);

现在您有了所需的行,在该行中搜索以查找编辑按钮(我可以看到只有一个锚点,因此最简单的搜索是tagname

deriredRow.FindElement(By.TagName("a")).Click();

如果您创建的功能仅用于单击编辑按钮,则如下所示:

public void ClickEdit(string locationName)
{
    driver.FindElements(By.TagName("tr"))
        .Where(row => row.FindElements(By.TagName("td"))[1] == locationName).Single()
        .FindElement(By.TagName("a")).Click();
}

答案 1 :(得分:1)

试试这个xpath:

driver.FindElement(By.XPath("//a[child::img[@title='Edit'] and parent::td[following-sibling::td[text()='" + locationName + "']]]")).Click();

答案 2 :(得分:0)

首先尝试识别“编辑”按钮并将其保存在列表中。然后使用循环依次单击它们。

List<WebElement> trList=driver.findElements(By.xpath("//table[@id='Your table ID here']/tr/td/a/img"));
                          (or)
List<WebElement> trList=driver.findElements(By.xpath("//img[@alt='Edit']"));
            for(int i=0;i<trList.size();i++){
                trList.get(i).click();
            }

答案 3 :(得分:0)

要点击表格中的修改 按钮,您可以使用以下代码行:

driver.FindElement(By.XPath("//a[@class='dxbButton_Office2010Silver dxgvCommandColumnItem_Office2010Silver dxgv__cci dxbButtonSys' and @id='officeRechargeGrid_DXCBtn3']")).Click();