1

我正在尝试查找文本,如果找到文本,然后单击Edit linktd 标记中的...。但是当您查看页面源时,我看不到如何确定要实现的任何建议或建议?

for an example:

在我的页面 html 源代码(见下文)中的 td 中,Title1一旦找到文本,我就会调用文本,然后我想找到一种方法来单击编辑链接(这是一个超链接)

这是我到目前为止所做的//这是我的代码

IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver();
string url ="http://site.com";
driver.Navigate().GoToUrl(url);
string pagesource = driver.PageSource;

//html页面源代码:

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head id="ctl00_PageHead">
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Employee </title>
    <body id="ctl00_PageBody">
        <form name="aspnetForm" method="post" action="login.aspx" id="aspnetForm" class="container">
        <div>
        </div>
        <div>
        </div>
        <table class="global-table" cellpadding="0" cellspacing="0">
            <tr class="body">
                <td>
                    <div id="body">
                        <div id="body-inner">
                            <h1>
                                Employee Information</h1>
                            <div id="ctl00_ContentPlaceHolder1_Control1_pnlList" style="width: 100%;">
                                &nbsp;<div class='filter'>
                                    Language:
                                    <select name="ctl00$ContentPlaceHolder1$Control1$ddlLanguage" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$Control1$ddlLanguage\',\'\')', 0)"
                                        id="ctl00_ContentPlaceHolder1_Control1_ddlLanguage">
                                        <option selected="selected" value="1">English</option>
                                    </select></div>
                                <div>
                                    <table class="gv" cellspacing="0" border="0" id="ctl00_ContentPlaceHolder1_Control1_gv"
                                        style="border-collapse: collapse;">
                                        <tr class="gridHeader">
                                            <td colspan="8">
                                                <div style="float: left; margin-left: 7px; margin-top: 0px;">
                                                    <div style="float: left;">
                                                        <input type="image" name="ctl00$ContentPlaceHolder1$Control1$gv$ctl01$ibtnFirst"
                                                            id="ctl00_ContentPlaceHolder1_Control1_gv_ctl01_ibtnFirst" disabled="disabled"
                                                            title="First Page" src="../App_Themes/Default/images/grid_page_first.png" style="border-width: 0px;
                                                            opacity: 0.4; filter: alpha(opacity=40); cursor: default;" />
                                                        <input type="image" name="ctl00$ContentPlaceHolder1$Control1$gv$ctl01$ibtnPrev" id="ctl00_ContentPlaceHolder1_Control1_gv_ctl01_ibtnPrev"
                                                            disabled="disabled" title="Previous Page" src="../App_Themes/Default/images/grid_page_prev.png"
                                                            style="border-width: 0px; opacity: 0.4; filter: alpha(opacity=40); cursor: default;" />
                                                    </div>
                                                    <div style="float: left; padding: 3px 5px 0px 5px;">
                                                        <div class="pagerNumber">
                                                            Page 1 of 34
                                                        </div>
                                                    </div>
                                                    <div style="float: left;">
                                                        <input type="image" name="ctl00$ContentPlaceHolder1$Control1$gv$ctl01$ibtnNext" id="ctl00_ContentPlaceHolder1_Control1_gv_ctl01_ibtnNext"
                                                            title="Next Page" src="../App_Themes/Default/images/grid_page_next.png" style="border-width: 0px;" />
                                                        <input type="image" name="ctl00$ContentPlaceHolder1$Control1$gv$ctl01$ibtnLast" id="ctl00_ContentPlaceHolder1_Control1_gv_ctl01_ibtnLast"
                                                            title="Last Page" src="../App_Themes/Default/images/grid_page_last.png" style="border-width: 0px;" />
                                                    </div>
                                                </div>
                                                <div style="float: left; margin: 0px 6px 0px 20px;">
                                                    Page Size
                                                    <select name="ctl00$ContentPlaceHolder1$Control1$gv$ctl01$ddlPageSize" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$Control1$gv$ctl01$ddlPageSize\',\'\')', 0)"
                                                        id="ctl00_ContentPlaceHolder1_Control1_gv_ctl01_ddlPageSize">
                                                        <option selected="selected" value="10">10</option>
                                                        <option value="20">20</option>
                                                        <option value="50">50</option>
                                                        <option value="100">100</option>
                                                    </select>
                                                </div>
                                                <div id="ctl00_ContentPlaceHolder1_Control1_gv_ctl01_RecordCount" style="float: right;
                                                    padding-right: 10px; margin-top: 3px;">
                                                    <b>10</b> Items Found</div>
                                            </td>
                                        </tr>
                                        <tr class="header">
                                            <th class=" nolink" scope="col">
                                                &nbsp;
                                            </th>
                                            <th scope="col">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Phone')">
                                                    Phone</a>
                                            </th>
                                            <th class=" sorted-desc" scope="col">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Title')">
                                                    Title</a>
                                            </th>
                                            <th scope="col">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$SubTitle')">
                                                    SubTitle</a>
                                            </th>
                                            <th scope="col">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Names')">
                                                    Names</a>
                                            </th>
                                            <th scope="col">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Names')">
                                                    Enames</a>
                                            </th>
                                            <th scope="col">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$Active')">
                                                    Active</a>
                                            </th>
                                            <th scope="col">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Sort$DateTime')">
                                                    &nbsp;DateTime</a>
                                            </th>
                                        </tr>
                                        <tr class="item">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$0')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                Title1
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                8/23/2011&nbsp;
                                            </td>
                                        </tr>
                                        <tr class="altItem">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$1')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                1
                                            </td>
                                            <td>
                                                1
                                            </td>
                                            <td>
                                                Employee
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                7/31/2014
                                            </td>
                                        </tr>
                                        <tr class="item">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$2')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                111
                                            </td>
                                            <td>
                                                111
                                            </td>
                                            <td>
                                                Employer
                                            </td>
                                            <td>
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                7/31/2013
                                            </td>
                                        </tr>
                                        <tr class="altItem">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$3')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                123
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                8/23/2011
                                            </td>
                                        </tr>
                                        <tr class="item">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$4')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                2
                                            </td>
                                            <td>
                                                2
                                            </td>
                                            <td>
                                                Employer
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                7/31/2012
                                            </td>
                                        </tr>
                                        <tr class="altItem">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$5')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                3
                                            </td>
                                            <td>
                                                3
                                            </td>
                                            <td>
                                                Employer&nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                7/31/2012
                                            </td>
                                        </tr>
                                        <tr class="item">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$6')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                a
                                            </td>
                                            <td>
                                                a
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                7/20/2012
                                            </td>
                                        </tr>
                                        <tr class="altItem">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$7')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                Day
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                Customer
                                            </td>
                                            <td>
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                3/2/2013
                                            </td>
                                        </tr>
                                        <tr class="item">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$8')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                a1
                                            </td>
                                            <td>
                                                a1
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                7/24/20/14
                                            </td>
                                        </tr>
                                        <tr class="altItem">
                                            <td align="center">
                                                <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Control1$gv','Select$9')">
                                                    Edit</a>
                                            </td>
                                            <td align="center" style="width: 15px;">
                                            </td>
                                            <td>
                                                A1
                                            </td>
                                            <td>
                                                A1
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                &nbsp;
                                            </td>
                                            <td>
                                                7/30/2012
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
            <tr class="footer">
                <td>
                </td>
            </tr>
        </table>
        </form>
    </body>
</html>
4

1 回答 1

2

为什么以下不起作用?

// Assumes "driver" is a valid object implementing IWebDriver
IWebElement table = driver.FindElement(By.ClassName("gv"));
IWebElement element = table.FindElement(By.XPath(".//td[contains(., 'Title1')]/..//a"));

本质上,这表示“从类名为“gv”的表开始,找到<td>文本包含“Title1”的元素,导航到它的父元素(应该是<tr>元素),然后找到第一个<a>后代元素。

于 2012-09-26T19:35:39.427 回答