I am trying to write a code that automatically clicks a link on a webpage by looking for the <a>
element and using InvokeMember("click")
. Here's what I've got so far:
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);
}
}
It's doing a great job at finding all the <a>
elements and saving each individual one. The problem is that it opens every link it "clicks" in a new window, and it always navigates to about:blank# including the #. Manually clicking the links in the webBrowser in the form also does this. Clicking the same link is Chrome does not.
How do I get it to not open the link in a new window, but always open it in webBrowser1
and get it to navigate to the right URL?
This is an example of a webpage I've tried it on:
<a href="#" onclick="redirect('51865fd774d8f419270045', '7020'); return false;" target="_blank" class="headline">
Link </a>
Also, the int count
is going to be used later on.