A HREF and AXwebbrowser

  • Thread starter Thread starter nicolas
  • Start date Start date
nicolas said:
Somebody knows how i can simulate a click on a <A HAREF> tag ?

Hi Nicolas,

//add a reference to the MSHTML Object Library

[...]

public Form1()
{
InitializeComponent();

// Wait until the document has finished loading
axWebBrowser1.DocumentComplete +=
new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(
axWebBrowser1_DocumentComplete);
}

private void button1_Click(object sender, System.EventArgs e)
{
//C# cannot deal with optional params
object miss = Type.Missing;
//goto google.com
axWebBrowser1.Navigate(
"http://www.google.com", ref miss, ref miss, ref miss, ref miss);
}

//is called when the browser has finished loading the page
private void axWebBrowser1_DocumentComplete(
object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
//get a reference to the HTML-document
mshtml.HTMLDocumentClass doc =
(mshtml.HTMLDocumentClass) axWebBrowser1.Document;

//foreach link in the HTML-document
foreach (mshtml.HTMLAnchorElementClass link in doc.links)
{
if (link.href == "http://www.google.de/grphp?hl=de&tab=wg")
{
//perform the click on the Google-Groups-Link
link.click();
}
}
}

Cheers

Arne Janning
 
thanks Arne, i will try it.


Arne Janning said:
nicolas said:
Somebody knows how i can simulate a click on a <A HAREF> tag ?

Hi Nicolas,

//add a reference to the MSHTML Object Library

[...]

public Form1()
{
InitializeComponent();

// Wait until the document has finished loading
axWebBrowser1.DocumentComplete +=
new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(
axWebBrowser1_DocumentComplete);
}

private void button1_Click(object sender, System.EventArgs e)
{
//C# cannot deal with optional params
object miss = Type.Missing;
//goto google.com
axWebBrowser1.Navigate(
"http://www.google.com", ref miss, ref miss, ref miss, ref miss);
}

//is called when the browser has finished loading the page
private void axWebBrowser1_DocumentComplete(
object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
//get a reference to the HTML-document
mshtml.HTMLDocumentClass doc =
(mshtml.HTMLDocumentClass) axWebBrowser1.Document;

//foreach link in the HTML-document
foreach (mshtml.HTMLAnchorElementClass link in doc.links)
{
if (link.href == "http://www.google.de/grphp?hl=de&tab=wg")
{
//perform the click on the Google-Groups-Link
link.click();
}
}
}

Cheers

Arne Janning
 
It's working ... thx

nicolas said:
thanks Arne, i will try it.


Arne Janning said:
nicolas said:
Somebody knows how i can simulate a click on a <A HAREF> tag ?

Hi Nicolas,

//add a reference to the MSHTML Object Library

[...]

public Form1()
{
InitializeComponent();

// Wait until the document has finished loading
axWebBrowser1.DocumentComplete +=
new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(
axWebBrowser1_DocumentComplete);
}

private void button1_Click(object sender, System.EventArgs e)
{
//C# cannot deal with optional params
object miss = Type.Missing;
//goto google.com
axWebBrowser1.Navigate(
"http://www.google.com", ref miss, ref miss, ref miss, ref miss);
}

//is called when the browser has finished loading the page
private void axWebBrowser1_DocumentComplete(
object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
//get a reference to the HTML-document
mshtml.HTMLDocumentClass doc =
(mshtml.HTMLDocumentClass) axWebBrowser1.Document;

//foreach link in the HTML-document
foreach (mshtml.HTMLAnchorElementClass link in doc.links)
{
if (link.href == "http://www.google.de/grphp?hl=de&tab=wg")
{
//perform the click on the Google-Groups-Link
link.click();
}
}
}

Cheers

Arne Janning
 
Sorry i have try it with yahoo.com and it does not work. I have an error
with "Le cast spécifié n'est pas valide" in french (the specify cast is
not valid). This error is with "mshtml.HTMLAnchorElementClass link in"
 
nico said:
Sorry i have try it with yahoo.com and it does not work. I have an error
with "Le cast spécifié n'est pas valide" in french (the specify cast is
not valid). This error is with "mshtml.HTMLAnchorElementClass link in"

Hi Nico,

then try it with

foreach (mshtml.IHTMLElement he in doc.links)
{
HTMLAnchorElementClass hae = he as HTMLAnchorElementClass;
...
}


Cheers

Arne Janning
 
Thanks, but can you explain me these lines ?

Arne Janning said:
Hi Nico,

then try it with

foreach (mshtml.IHTMLElement he in doc.links)
{
HTMLAnchorElementClass hae = he as HTMLAnchorElementClass;
...
}


Cheers

Arne Janning
 
it does not work ... i have a System.NullReferenceException error.
hae is not defined value

My script his :

private void button1_Click(object sender, System.EventArgs e)

{

string URL = textBox1.Text;

//C# cannot deal with optional params

object miss = Type.Missing;

//goto google.com

axWebBrowser1.Navigate(

URL, ref miss, ref miss, ref miss, ref miss);

}

private void axWebBrowser1_Enter(object sender, System.EventArgs e)

{


}


private void textBox1_TextChanged(object sender, System.EventArgs e)

{


}

private void button2_Click(object sender, System.EventArgs e)

{

//get a reference to the HTML-document

mshtml.HTMLDocumentClass doc =

(mshtml.HTMLDocumentClass) axWebBrowser1.Document;

//foreach link in the HTML-document

foreach (mshtml.IHTMLElement he in doc.links)

{

HTMLAnchorElementClass hae = he as HTMLAnchorElementClass;

textBox2.Text=hae.href;

}

}

private void textBox2_TextChanged(object sender, System.EventArgs e)

{


}
 
nicolas said:

Hi nicolas,

IE-Automation in C#...

.... it's all about trial&error... ;-)

This one is working for me:

public Form1()
{
InitializeComponent();
axWebBrowser1.DocumentComplete +=
new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(
axWebBrowser1_DocumentComplete);
}

private void button1_Click(object sender, System.EventArgs e)
{
//C# cannot deal with optional params
object miss = Type.Missing;
//goto yahoo.com
axWebBrowser1.Navigate(
"http://www.yahoo.com", ref miss, ref miss, ref miss, ref miss);
}

private void axWebBrowser1_DocumentComplete(
object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
//get a reference to the HTML-document
mshtml.HTMLDocumentClass doc =
(mshtml.HTMLDocumentClass) axWebBrowser1.Document;

//foreach link in the HTML-document
foreach (mshtml.IHTMLElement link in doc.links)
{
MessageBox.Show(
link.getAttribute("href", 0).ToString());
}
}

Cheers

Arne Janning

(Let's try if we can call scripts in a webbrowser-control from C# as
well ;-)
 
Cool it's working but now how can i click on on of these links.

HTML link on the site is like that :

<div align="center" id="divSponsors">
<form name='AffComb'><table width="480" bordercolor="#FFFFFF" border="0"
cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#E0C8AF" height="285" width="478" align="center">
<A
HREF="javascript:valide(498365,'CD_DISCOUNT_KOODPO','http://oas.koodpo.com/R
ealMedia/ads/adstream.cap/1626338510?c=kc10&dv=1&e=30d&u=http://oas.koodpo.c
om/RealMedia/ads/click_lx.ads/www.koodpo.com/confirmation/1626338510/Positio
n1/CD_DISCOUNT_KOODPO/discount.html/64393133333463353431306164393530','<!--
vide --><a
href=#quote#http://oas.koodpo.com/RealMedia/ads/adstream.cap/1626338510?c=kc
10&dv=1&e=30d&u=http://oas.koodpo.com/RealMedia/ads/click_lx.ads/www.koodpo.
com/confirmation/1626338510/Position1/CD_DISCOUNT_KOODPO/discount.html/64393
133333463353431306164393530#quote# target=#quote#_blank#quote#><img
src=#quote#http://www.cibleclick.com/cibles/banniere/symp.cfm?site_id=663151
718&friend_id=604261945&banniere_id=13110#quote#
border=#quote#0#quote#></a>')">
<img
src="http://www.cibleclick.com/cibles/banniere/symp.cfm?site_id=663151718&fr
iend_id=604261945&banniere_id=13110" border="0"></A>
</td>
</tr>
</table>
<input type="hidden" name="banniere" value="" >
<input type="hidden" name="campagne" value="" >
<input type="hidden" name="url" value="" >
</form>
</div>

Have you a tutorial or a book about that, mshtml ?
 
YES YES YES i have found ..... ;o)))

mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)
axWebBrowser1.Document;

doc.parentWindow.execScript("func();","javascript");
 
Back
Top