var ie = new InternetExplorer { Visible = true };
ie.Navigate("http://google.com");
ie.DocumentComplete += IeDocumentComplete;
But when the document is opened, it's like a Ctrl+N in Internet Explorer.
I don't want to keep context and session. I would like a new instance of Internet explorer.
Can you help me please ?
Many Thanks
I think you need to capture the key board event and pass it to the InternetExpolorer control, if I am not wrong because the key interrupts are sent to your application (and not to your IE Control) it does not work.
http://msdn.microsoft.com/en-us/library/aa768335(VS.85).aspx
Hope that helps!!!
Based on your example code, I'm guessing your using the InternetExplorer class from ShDocVw.dll?
Instead of using that, try to open Internet Explorer by launching it as a new process...
System.Diagnostics.Process.Start("iexplore.exe http://www.google.com");
That may do it, but it may also be down to specific user settings in the browser itself.
Related
I'm trying to connect to an Internet Explorer window that is already open. Once connected I need to send some keystrokes (via SendKeys) to the IE window for some processing. I've got the following code below that works up until the SendKeys command. It finds the IE window titled "Graphics Database". When it hits "SendKeys.Send("{TAB}");" I get the error "An unhandled exception of type 'System.NullReferenceException' occurred".
Additional information: I also get the following on the NullReferenceException error. The weird thing is if I code to open a new IE window and then use SendKeys it works fine. Connecting to an existing windows seems to cause this issue.
SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method.
Can anyone please help me figure out what to do to fix this?
Andy
InternetExplorer IE = null;
// Get all browser objects
ShellWindows allBrowsers = new ShellWindows();
if (allBrowsers.Count == 0)
{
throw new Exception("Cannot find IE");
}
// Attach to IE program process
foreach (InternetExplorer browser in allBrowsers)
{
if (browser.LocationName == "Graphics Database")
{
MessageBox.Show ("Found IE browser '" + browser.LocationName + "'");
IE = (InternetExplorer)browser;
}
}
IE.Visible = true;
System.Threading.Thread.Sleep(2000);
SendKeys.Send("{TAB}");
SendKeys.Send("G1007");
SendKeys.Send("{ENTER}");
I was able to resolve this issue. I could never get the IE.Visible = true to work. This seemed to do nothing in my code. I had to use the SetForegroundWindow() to set the focus to the IE window.
// Find the IE window
int hWnd = FindWindow(null, "Graphics Database - Internet Explorer");
if (hWnd > 0) // The IE window was found.
{
// Bring the IE window to the front.
SetForegroundWindow(hWnd);
This site helped me immensely with getting the SetForegroundWindow() working.
http://forums.codeguru.com/showthread.php?460402-C-General-How-do-I-activate-an-external-Window
Andy please bear with me as this will be long. First you are going to want to look mshtml documentation and Dom. https://msdn.microsoft.com/en-us/library/aa741314(v=vs.85).aspx I don't know why automation is so convoluted but it is. The UIautomation class works great for windows apps but has nothing really for IE that I've been able to find. Others will point to third parties like waitn and selenium. Waitn appears to no longer be supported and selenium won't let you grab an open IE browser. I have gone down this path recently because I wanted to be able to create an app to store my web passwords and auto fill them in since I can't save my username and passwords in browser due to security restrictions. I have an example here and hope it helps. First open up IE and navigate to http://aavtrain.com/index.asp. Then have a console project with mshtml referenced and shdocvw. Here is code below. It gets the window then finds elements for username, password, and submit. then populates the username and password and clicks the submit button. I don't have a login to this site so it won't log you in. I have been using it for my testing. Problem I have is sites with javascript login forms. If you get further with this info please post back as I am still trying to evolve the concepts and create something reusable.
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
Console.WriteLine("Starting Search\n\n\n");
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
if (ie.LocationURL.Contains("aavtrain"))
{
Console.WriteLine(ie.LocationURL);
Console.WriteLine("\n\n\n\n");
Console.WriteLine("FOUND!\n");
mshtml.HTMLDocument document = ie.Document;
mshtml.IHTMLElementCollection elCol = document.getElementsByName("user_name");
mshtml.IHTMLElementCollection elCol2 = document.getElementsByName("password");
mshtml.IHTMLElementCollection elCol3 = document.getElementsByName("Submit");
Console.WriteLine("AutofillPassword");
foreach (mshtml.IHTMLInputElement i in elCol)
{
i.defaultValue = "John";
}
foreach (mshtml.IHTMLInputElement i in elCol2)
{
i.defaultValue = "Password";
}
Console.WriteLine("Will Click Button in 2 seconds");
Thread.Sleep(2000);
foreach (mshtml.HTMLInputButtonElement i in elCol3)
{
i.click();
}
}
}
Console.WriteLine("Finished");
I'm new to C# and I'm trying to do an application that automatize Internet Explorer.
When I click a button, the application does :
using ( var Browser = new IE())
{
Browser.GoTo("http://testweb.com");
Browser.TextField(Find.ByName("username")).TypeText("User");
Browser.TextField(Find.ByName("password")).TypeText("Pass");
}
But it doesn't write anything. It navigates to the web but...
Try this:
IE ie = null;
ie = new IE();
ie.GoTo("Link");
ie.WaitForComplete();
At least to get started.
For the other bit, you need to get an exact identification and then you can tell WaTiN to interact with it.
Textfield userTextBox = ie.Textfield(Find.ByName("name"));
userTextBox.TypeText("user");
This may seem banal but now you can add a peek definition in your code and see if "userTextBox" gets found by name. If it doesn't you need to find it through another method (ID or class).
I'am running across this issue when I'm debugging or running my coded UI automation project, where i get the exception labeled "{"COM object that has been separated from its underlying RCW cannot be used." System.Exception {System.Runtime.InteropServices.InvalidComObjectException}" everytime i come from a browser window that contains a pdf reader embedded in it. This happens every time I retrieve the window and try to click back. It barfs when i perform the back method on it. I've tried different things but none has worked including the playback wait.
var hereIsmypdf = ReturnPDFDoc();
public BrowserWindow ReturnPDFDoc()
{
Playback.Wait(1000);
var myPdFdoc = GlobalVariables.Browser;
return myPdFdoc;
}
hereIsmypdf.Back();
The only way i was able to get around this issue was not to use the BrowserWindow class. I ended up using the WinWindow class and just getting the tab of the window from it. The BrowserWindow class seemed to trigger the exception "COM object that has been separated from its underlying RCW cannot be used." System.Exception {System.Runtime.InteropServices.InvalidComObjectException}" everytime i tried to retrieve it. I hope this helps someone one or maybe someone has a better way to handle this issue.
For the people that voted my question down, i really did try to figure it out. Sorry i wasnt clear about what i was asking the community or couldn't properly articulate what this pain was. I'm sure someone probably is going through the same pain i did and having a hard time articulating whats going on.
Here is my code on what i ended up doing
public WinTabPage ReturnPDFDoc()
{
WinWindow Wnd = new WinWindow();
Wnd.SearchProperties[BrowserWindow.PropertyNames.ClassName] = "IEFrame";
WinTabList tabRoWlist = new WinTabList(Wnd);
tabRoWlist.SearchProperties[WinTabPage.PropertyNames.Name] = "Tab Row";
WinTabPage myTab = new WinTabPage(tabRoWlist);
myTab.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
myTab.SearchProperties[WinTabPage.PropertyNames.Name] = "something";
//UITestControlCollection windows = newWin.FindMatchingControls();
return myTab;
}
I'm currently trying to use WatiN to do some automatic data collection. I used to use a WebBrowser controll, and the way I did it was I declared a stathread and ran it from there, then using a Browser.DocumentCompleted I started a void LoginPageLoaded on which I would set the user and pass, and login. Thing is, with WatiN, I'm trying to do
var th = new Thread(() =>
{
Browser browser = new IE(url);
browser.WaitForComplete();
HolyThunder hl = new HolyThunder();
l.LoginPageLoaded();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
But obviously, when I try to use the browser instance on the LoginPageLoaded void it says it downs't know what it is, because it was declared inside the th thread. I didn't do it when I ran LoginPageLoaded through browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LoginPageLoaded);, and I obviously can't declare the browser outside of that STAThread. What do you think I can do to fix this?
EDIT - I failed so bad... if I do everything I need to do inside that STAthread, everything will work. My question now is, in watin, is there anyway to open a "webbrowser" without actually having the visual component of the webbrowser?
You can try using NHtmlUnit, it's a headless browser. I am not sure if you can do it with WatiN.
You can run WebKit headless (see, for example http://phantomjs.org/), but I don't know if you can drive it from WatiN.
I guess you want to hide web browser window.
In that case try this:
Settings.Instance.MakeNewIeInstanceVisible = false;
To open a "webbrowser" without actually having the visual component of the webbrowser, use
var browser = new IE();
browser.Visible = false;
This question is more of a follow-up to this one:
Hiding Internet Explorer when WatiN is run
Like the person who asked that original question, I also want to stop IE from being shown when my WatiN tests are running, but even when using this setting in a seemingly correct manner (code snippet below), it still ends up showing an empty IE window initially (although it does not show the test behavior/web page interaction).
Is it possible to stop the window from showing at all, or is this as good as it gets?
My helper method to create a new IE instance:
public static IE CreateNewBrowserInstance(string url = DefaultAppUrl)
{
Settings.Instance.MakeNewIeInstanceVisible = false;
Settings.Instance.AutoMoveMousePointerToTopLeft = false;
Settings.Instance.AutoStartDialogWatcher = false;
return new IE(url, true);
}
You can Hide window after initializing new IE instance
browser.ShowWindow(NativeMethods.WindowShowStyle.Hide);