导航网站时,指定的参数超出了有效值范围

时间:2015-06-30 18:21:26

标签: winforms visual-studio-2010 c#-4.0

在下面的代码中,我有一个页面加载。一旦完成加载,我就会登录该站点。登录完成并加载下一页后,我需要点击一个链接。

问题是链接尝试在登录后的页面完成加载之前单击。

我得到的错误是:指定的参数超出了有效值的范围。 参数名称:index

    int linkcount = 0;

    public PNow()
    {
        InitializeComponent();
        webBrowser1.Navigate("https:...com/login.aspx");
    }

    private void PNow_Load(object sender, EventArgs e)
    {

    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        linkcount++;

        if (linkcount == 1)
        {
            webBrowser1.Document.GetElementById("Login1_UserName").SetAttribute("value", "username");
            webBrowser1.Document.GetElementById("Login1_Password").SetAttribute("value", "password");
            webBrowser1.Document.GetElementById("Login1_LoginButton").InvokeMember("click");
        }

        if (linkcount == 2)
        {
            HtmlElement link = (from HtmlElement elem in webBrowser1.Document.GetElementsByTagName("a")
            where elem.GetAttribute("title") == "Time Clock Entry"
            select elem).ElementAt(0);
        }
    }

我尝试过使用Thread.Sleep(5000);但这只是停止整个程序,问题仍然存在。

谢谢。

包含链接的页面部分的标记如下...

<ul>
<li id="menuLi_2708"><a  class="noTimer container-menu" href="javascript:void(0)" onclick="showFirstChildMenu(this)">Career Development</a><ul>
<li id="menuLi_2708"><a  href="#" class="page-link" onclick="showMenu('pages/EDIT/CDAvailableDevOppSummary.aspx?USParams=PK=ESS!MenuID=2708!PageRerId=2711!ParentRerId=2708','72','2708','2711', false, false, this.innerHTML, true);" data-RerID='2711'>Available</a></li>
</ul>
<ul>
<li id="menuLi_2708"><a  href="#" class="page-link" onclick="showMenu('pages/EDIT/CDCurrentDevOppSummary.aspx?USParams=PK=ESS!MenuID=2708!PageRerId=2732!ParentRerId=2708','72','2708','2732', false, false, this.innerHTML, true);" data-RerID='2732'>Current</a></li>
</ul>
<ul>
<li id="menuLi_2708"><a  href="#" class="page-link" onclick="showMenu('pages/EDIT/CDCompletedDevOppSummary.aspx?USParams=PK=ESS!MenuID=2708!PageRerId=2735!ParentRerId=2708','72','2708','2735', false, false, this.innerHTML, true);" data-RerID='2735'>Completed</a></li>
</ul>
</li>
</ul>
<ul>
<li id="menuLi_2148"><a  href="#" class="page-link" onclick="showMenu('pages/VIEW/UTMSSO.aspx?USParams=PK=ESS!MenuID=2148!PageRerId=2148!ParentRerId=72','72','2148','2148', false, false, this.innerHTML, true);" data-RerID='2148'>Time Management</a></li>
</ul>
<ul>
<li id="menuLi_2147"><a  href="#" class="page-link" onclick="showMenu('pages/VIEW/UTMEntry.aspx?USParams=PK=ESS!MenuID=2147!PageRerId=2147!ParentRerId=72','72','2147','2147', false, false, this.innerHTML, true);" data-RerID='2147'>Time Clock Entry</a></li>
</ul>
<ul>
<li id="menuLi_95"><a  class="noTimer container-menu" href="javascript:void(0)" onclick="showFirstChildMenu(this)">Pay</a><ul>
<li id="menuLi_95"><a  href="#" class="page-link" onclick="showMenu('pages/VIEW/EePayrollPayCheckDetail.aspx?USParams=PK=ESS!MenuID=95!PageRerId=1417!ParentRerId=95','72','95','1417', false, false, this.innerHTML, true);" data-RerID='1417'>Current Pay Statement</a></li>
</ul>
<ul>
<li id="menuLi_95"><a  href="#" class="page-link" onclick="showMenu('pages/VIEW/EePayrollPayCheckHistory.aspx?USParams=PK=ESS!MenuID=95!PageRerId=18!ParentRerId=95','72','95','18', false, false, this.innerHTML, true);" data-RerID='18'>Pay History</a></li>
</ul>
<ul>
<li id="menuLi_95"><a  href="#" class="page-link" onclick="showMenu('pages/VIEW/EePayrollYTDSummary.aspx?USParams=PK=ESS!MenuID=95!PageRerId=20!ParentRerId=95','72','95','20', false, false, this.innerHTML, true);" data-RerID='20'>YTD Summary</a></li>
</ul>
<ul>
<li id="menuLi_95"><a  href="#" class="page-link" onclick="showMenu('pages/VIEW/WPRThirdPartyPayHistory.aspx?USParams=PK=ESS!MenuID=95!PageRerId=3561!ParentRerId=95','72','95','3561', false, false, this.innerHTML, true);" data-RerID='3561'>Third Party Pay</a></li>
</ul>

1 个答案:

答案 0 :(得分:0)

这个区块对我来说非常可疑:

if (linkcount == 2)
{
    HtmlElement link = (from HtmlElement elem in webBrowser1.Document.GetElementsByTagName("a")
    where elem.GetAttribute("title") == "Time Clock Entry"
    select elem).ElementAt(0);
}

看起来你没有一个名为title的属性的元素,在调用它们上面的ToString()后,评估为&#34;时钟输入&#34;。因此,序列包含零元素,这会导致抛出ArgumentOutOfRangeException。

我仔细检查这个区块,并仔细地逐步完成。在调试器中设置一些手表。使用立即窗口。仔细检查你在那里看到的东西。很可能,你不会拉回你的想法。

<强>更新

这是你的超链接:

<a href="#" 
    class="page-link" 
    onclick="showMenu('pages/VIEW/UTMEntry.aspx?USParams=PK=ESS!MenuID=2147!PageRerId=2147!ParentRerId=72','72','2147','2147', false, false, this.innerHTML, true);" 
    data-RerID='2147'>
    Time Clock Entry
</a>

请注意,您没有属性评估为&#34;时钟输入&#34;。相反,元素的 InnerHtml InnerText 属性评估为&#34;时钟输入&#34;。

按如下方式更改您的代码:

if (linkcount == 2)
{
    HtmlElement link = (from HtmlElement elem in webBrowser1.Document.GetElementsByTagName("a")
    where elem.InnerHtml == "Time Clock Entry"
    select elem).ElementAt(0);
}

这应该让你去。