I am using the following C# code to automatically populate and validate a web page:
namespace Web_Browser_Manipulation { public partial class Form1 : Form { AutoResetEvent waitHandle = new AutoResetEvent(false); public Form1() { InitializeComponent(); } public void ie_DocumentComplete(object pDisp, ref object URL) { InternetExplorer ie = pDisp as InternetExplorer; ie.DocumentComplete -= ie_DocumentComplete; waitHandle.Set(); } private void UploadSignature(string Nome_SW, string Codice_SW, string Codice_Conc, string Codice_Gioco) { foreach (InternetExplorer ie in new ShellWindows()) { if (ie.LocationURL.Contains("aams")) { int index = FindLineIndex(Codice_Conc, Codice_Gioco); ie.DocumentComplete += ie_DocumentComplete; if (index != -1) { ie.Document.GetElementsByTagName("option")[index].SetAttribute("selected", "true"); ie.Document.GetElementsByTagName("input")["aggiungi"].Click(); ie.Document.GetElementById("num").SetAttribute("value", "1"); ie.Document.GetElementsByTagName("input")["prepara"].Click(); ie.Document.GetElementById("nome_sftw1").SetAttribute("value", Nome_SW); ie.Document.GetElementById("cod_sha11").SetAttribute("value", Codice_SW); ie.Document.GetElementsByTagName("input")["Acquisisci"].Click(); } } } } private void button1_Click(object sender, EventArgs e) { UploadSignature("Nome1", "1234567890123456789012345678901234567890", "15023", "1"); waitHandle.WaitOne(); UploadSignature("Nome2", "1234567890123456789012345678901234567891", "15023", "1"); waitHandle.WaitOne(); } }
The code works great, however, when the function "UploadSignature" is called the 2nd time, the "InternetExplorer ie" fails and therefore the program crashes (it raises an exception:'(((SHDocVw.InternetExplorer)(ie)).Document).baseUrl' generated 'System.NotImplementedException' and fileCreatedDate = '(((SHDocVw.InternetExplorer)(ie)).Document).fileCreatedDate' generaed 'System.Runtime.InteropServices.COMException'). When the program reaches the line
ie.Document.GetElementsByTagName("option")[index].SetAttribute("selected","true");
it crashes.
I really do not mange to figure out why. Could anyone help? Thank you so much for any hint you might provide!