Web浏览器查找锚和c#中的自动点击

本文关键字:浏览器 查找 Web | 更新日期: 2023-09-27 18:11:43

我正在创建一个我使用c#浏览器控件的工具,它会自动登录IRCTC并预订机票。一切正常,除了一页。我不能通过浏览器点击,我怎么能找到锚并通过c#浏览器点击。下面是HTML:

<table class="rf-dt width100" id="avlAndFareForm:trainbtwnstns">
            <colgroup span="16">
            </colgroup>
            <thead class="rf-dt-thd" id="avlAndFareForm:trainbtwnstns:th">
                <tr class="rf-dt-shdr" id="avlAndFareForm:trainbtwnstns:ch">
                    <th id="avlAndFareForm:trainbtwnstns:j_idt261" scope="col" class="rf-dt-shdr-c">
                        Train No.
                        <div id="up" style="float: right; display: none;">
                        </div>
                        <div id="down" style="float: right; display: none;">
                        </div>
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt265" scope="col" class="rf-dt-shdr-c">
                        Train Name
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt268" scope="col" class="rf-dt-shdr-c">
                        From
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt271" scope="col" class="rf-dt-shdr-c">
                        Departure
                        <div id="up" style="float: right; display: block;" class="up-hdr">
                        </div>
                        <div id="down" style="float: right; display: none;">
                        </div>
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt275" scope="col" class="rf-dt-shdr-c">
                        To
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt278" scope="col" class="rf-dt-shdr-c">
                        Arrival
                        <div id="up" style="float: right; display: none;">
                        </div>
                        <div id="down" style="float: right; display: none;">
                        </div>
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt282" scope="col" class="rf-dt-shdr-c">
                        Dist.(Km)
                        <div id="up" style="float: right; display: none;">
                        </div>
                        <div id="down" style="float: right; display: none;">
                        </div>
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt286" scope="col" class="rf-dt-shdr-c">
                        Travel Time
                        <div id="up" style="float: right; display: none;">
                        </div>
                        <div id="down" style="float: right; display: none;">
                        </div>
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt290" scope="col" class="rf-dt-shdr-c">
                        M
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt293" scope="col" class="rf-dt-shdr-c">
                        T
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt296" scope="col" class="rf-dt-shdr-c">
                        W
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt299" scope="col" class="rf-dt-shdr-c">
                        T
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt302" scope="col" class="rf-dt-shdr-c">
                        F
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt305" scope="col" class="rf-dt-shdr-c">
                        S
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt308" scope="col" class="rf-dt-shdr-c">
                        S
                    </th>
                    <th id="avlAndFareForm:trainbtwnstns:j_idt311" scope="col" class="rf-dt-shdr-c">
                        Class
                    </th>
                </tr>
            </thead>
            <tbody class="rf-dt-b" id="avlAndFareForm:trainbtwnstns:tb">
                <tr class="rf-dt-r" id="avlAndFareForm:trainbtwnstns:9">
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:9:j_idt261">
                        <a onclick="trainScheFunc('12141','CSTM','Thu Jul 03 00:00:00 IST 2014')" href="#">12141</a>
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt265">
                        RAJENDRA NGR EXP
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt268">
                        CSTM
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt271">
                        23:25
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt275">
                        JBP
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt278">
                        14:15
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt282">
                        997
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt286">
                        14:50
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt290">
                        Y
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt293">
                        Y
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt296">
                        Y
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt299">
                        Y
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt302">
                        Y
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt305">
                        Y
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt308">
                        Y
                    </td>
                    <td class="rf-dt-c" id="avlAndFareForm:trainbtwnstns:6:j_idt311">
                        <a onclick="availFareEnq(this,'12141','Thu Jul 03 00:00:00 IST 2014','1A','CSTM','JBP',true)">
                            1A</a> <a onclick="availFareEnq(this,'12141','Thu Jul 03 00:00:00 IST 2014','2A','CSTM','JBP',true)">
                                2A</a> <a onclick="availFareEnq(this,'12141','Thu Jul 03 00:00:00 IST 2014','3A','CSTM','JBP',true)">
                                    3A</a> <a onclick="availFareEnq(this,'12141','Thu Jul 03 00:00:00 IST 2014','SL','CSTM','JBP',true)">
                                        SL</a> <span id="ttf" style="display: none;">O,</span>
                    </td>
                </tr>
            </tbody>
            <tbody id="avlAndFareForm:trainbtwnstns:sc">
                <tr>
                    <td style="display: none">
                        <script type="text/javascript">                        new RichFaces.ui.DataTable("avlAndFareForm:trainbtwnstns", { "ajaxEventOptions": {} })</script>
                    </td>
                </tr>
            </tbody>
        </table>
我想通过浏览器点击下面的

。我找不到下面的锚:请帮助我:

<a onclick="availFareEnq(this,'12141','Thu Jul 03 00:00:00 IST `2014','SL','CSTM','JBP',true)"> SL</a>.

Web浏览器查找锚和c#中的自动点击

你可以在浏览器控件加载的页面中注入html并加载jquery,然后注入javascript,就像使用jquery找到锚标记并点击它一样。

HtmlElement head = idOfBrowserControl.Document.GetElementsByTagName("head")[0];
HtmlElement script1 = idOfBrowserControl.Document.CreateElement("script");
script.SetAttribute("type", "text/javascript");
script.SetAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js");
HtmlElement script2 = idOfBrowserControl.Document.CreateElement("script");
script.SetAttribute("type", "text/javascript");
script.InnerHtml = "function clickAnchor() { $($('a', '#avlAndFareForm:trainbtwnstns:6:j_idt311')[0]).click();}}";
head.AppendChild(script1);
head.AppendChild(script2);

当你想点击它时,点击

idOfBrowserControl.Document.InvokeScript("clickAnchor");

你不需要注入jQuery,我这样做只是因为我使用jQuery的一切…它是容易。