InvokeMember(“click”)总是打开一个新窗口并导航到about:blank#

时间:2013-05-05 13:59:45

标签: c# hyperlink click browser invokemember

我正在尝试编写一个代码,通过查找<a>元素并使用InvokeMember("click")自动点击网页上的链接。这是我到目前为止所得到的:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(fetchRandom(filetxt.Text));
                CookieContainer cookieContainer = new CookieContainer();
                webRequest.CookieContainer = cookieContainer;
                webRequest.AllowAutoRedirect = false;
                //webRequest.Proxy = new WebProxy(host, port);

                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                Stream receiveStream = response.GetResponseStream();

                if (!webBrowser1.IsDisposed)
                {
                    webBrowser1.ScriptErrorsSuppressed = true;
                    webBrowser1.DocumentStream = receiveStream;
                    Delay(delayint);

                    if (!webBrowser1.IsDisposed)
                    {

                        HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("a");

                        //int count = 0;
                        foreach (HtmlElement link in links)
                        {

                        }

                        Delay(delayint);
                    }
                }

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(fetchRandom(filetxt.Text));
                CookieContainer cookieContainer = new CookieContainer();
                webRequest.CookieContainer = cookieContainer;
                webRequest.AllowAutoRedirect = false;
                //webRequest.Proxy = new WebProxy(host, port);

                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                Stream receiveStream = response.GetResponseStream();

                if (!webBrowser1.IsDisposed)
                {
                    webBrowser1.ScriptErrorsSuppressed = true;
                    webBrowser1.DocumentStream = receiveStream;
                    Delay(delayint);

                    if (!webBrowser1.IsDisposed)
                    {

                        HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("a");

                        int count = 0;
                        foreach (HtmlElement link in links)
                        {
                            if (count == 0)
                            {
                                 link.InvokeMember("click");
                                 count = count + 1
                            }
                        }

                        Delay(delayint);
                    }
                }

它在找到所有<a>元素并保存每个元素方面做得很好。问题是,它会打开它在新窗口中“点击”的每个链接,并始终导航到 about:blank#,包括#。手动单击表单中webBrowser中的链接也可以执行此操作。点击相同的链接是Chrome没有。

如何让它在新窗口中不打开链接,但始终在webBrowser1中打开并让它导航到正确的URL?

这是我尝试过的网页示例:

<a href="#" onclick="redirect('51865fd774d8f419270045', '7020'); return false;" target="_blank" class="headline">
            Link            </a>

此外,int count将在稍后使用。

0 个答案:

没有答案