使用selenium WebDriver c#基于名称单击超链接的最佳方法是什么?

时间:2012-11-05 18:38:53

标签: webdriver selenium-webdriver

下面是html代码,它是我的.html页面的结构。我有一个很长的页面,但出于示例目的,我只有三行,而在实际页面中我有数百个

我的问题是:完成以下任务的最佳方法是什么:

如何点击Add to Basket获取员工姓名John Steven(或任何其他名称)?

 <table class="gvlist" cellspacing="0" border="0" id="ctl00_ctl00_Features_Features_ContentList_gv">
        <tr class="gvheader">
            <th>
                Employee Name
            </th>
            <th>
                Topic
            </th>
            <th>
                Add Employee
            </th>
        </tr>
        <tr class="gvrow">
            <td>
                <a id="ctl00_employee_List_gv_ctl02_lnkPage" class="gvtitle" href="#" target="_blank">
                    Erich Schulz</a>
            </td>
            <td>
                <ul class="topiclist">
                    <li class="ListItem" style="list-style: none"><a id="ctl00_employee_List_gv_ctl02_lbTopic"
                        class="InlineItemText" href="#">R&D</a> </li>
                </ul>
            </td>
            <td>
                <a id="ctl00_employee_List_gv_ctl02_lnkSelect" href="#" class="addpage">Add to Basket
                    List</a>
            </td>
        </tr>
        <tr class="gvrow">
            <td>
                <a id="ctl00_employee_List_gv_ctl03_lnkPage" class="gvtitle" href="#" target="_blank">
                    John Steven</a>
            </td>
            <td>
                <ul class="topiclist">
                    <li class="ListItem" style="list-style: none"><a id="A2" class="InlineItemText" href="#">
                        Development</a> </li>
                </ul>
            </td>
            <td>
                <a id="ctl00_employee_List_gv_ctl03_lnkSelect" href="#" class="addpage">Add to Basket
                    List</a>
            </td>
        </tr>
        <tr class="gvrow">
            <td>
                <a id="ctl00_employee_List_gv_ctl04_lnkPage" class="gvtitle" href="#" target="_blank">
                    John McAfee</a>
            </td>
            <td>
                <ul class="topiclist">
                    <li class="ListItem" style="list-style: none"><a id="A5" class="InlineItemText" href="#">
                        Tech</a> </li>
                </ul>
            </td>
            <td>
                <a id="ctl00_employee_List_gv_ctl04_lnkSelect" href="#" class="addpage">Add to Basket
                    List</a>
            </td>
        </tr>
    </table>

2 个答案:

答案 0 :(得分:2)

可能有很多可能性,但我会选择xpath

.//a[contains(.,'John McAfee')]/ancestor::tr//a[@class='addpage']

其中'John McAfee'可以通过变量传递

答案 1 :(得分:0)

试试这个

driver.findElement(By.linkText(“Add to Basket”))。click();