我正在尝试编写一个代码,通过查找<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
后面会用到。