Selenium how to close Internet Explorer has stopped working - c#

I am screen scraping a legacy website that uses activex controls. I am using selenium to accomplish this.
This website is buggy and whenever you signout of it, it crashes IE and you get the "Internet Explorer has stopped working" window. This happens no matter what, website needs to be recoded.
Is there a way in InternetExplorerDriver.Quit() to detect if this window is up and close it? The annoyance is that while the dialog is doing "searching for a solution..." it doesn't release selenium, so my code hangs up waiting.
Right now I am attempting to kill the process through the windows api, but that requires waiting for selenium to finish closing.

The solution Richard posted in the comments worked. I just told windows to stop checking for solutions.

Related

Selenium ChromeDriver - Run in background but not headless

I am looking for a way to run selenium tests with a chrome driver, in the background. With the background I mean as in, not the foreground focussed window. I can actually do this, but as soon as actions like opening a new tab or switching between windows (so basically switching window handles) happen in the chrome driver, the browser window gets pushed to the foreground.
So my question is, how can I prevent this from happening without running the test headless?
Any suggestions are appreciated, open for discussion.
EDIT
As a somewhat temporary solution I came up with the following.
Using the Windows 10 Virtual Desktops feature, I run the test and thus the chrome browser window in a seperate virual desktop.
I then switch back to my main virtual desktop to continue with other tasks.
This prevents the chrome browser window from being forced to the foreground.
Note that this still makes a flashing chrome icon appear in the taskbar when any of the actions described above appear.
Still looking for a more solid solution, so any suggestions are still appreaciated.
To hopefully open new perspectives and discussion points, and ultimately a solution, I will provide some more detailed info of what my code is doing.
I have 5 chrome webdrivers, and each of these webdrivers contains 6 tabs (WindowHandles).
The idea is that a certain process has to be repeated continuously on each tab. So we loop over each webdriver, and within that webdriver over each tab and set this tab as the webdrivers current WindowHandle. This makes the chrome window visibly switch to the assigned tab.
After that switch has taken place, so basic selenium automation is performed on the content of the tab, after wich we repeat the whole process.
The actual issue seems to take place when a chrome webdriver switches to a new tab (WindowHandle), at this moment the chrome window containing the tab is pushed to the foreground and steals focus. Note that this does not always takes place, often it can switch tabs without any issues. So it is unclear wether there is another factor which would cause the window to steal focus.
EDIT 2
After doing the following:
I overloaded the selenium method which is used to switch between tabs (WindowHandles), and called SetWindoPos each time. Unfortunately this did not solve this issue either. I will try to look deeper into what might be causing this and will report back. – S. Van den Wyngaert
I went out for a few hours while running the tests, and came back to see that surprisingly I was still on my main Virtual Desktop (win10 feature). This means that the issue didn't occur during the time I was gone. I started working again, opened a chrome window and noticed that shortly after I did this, focus was stolen by one of the chrome driver's windows again.
What I conclude from this is that the issue only occurs when another chrome window (not opened by a chrome driver from code) is opened.
I will keep investigating and will report back with updates.
After investigating this behavior for a few more hours I noticed that when another chrome window is open, as long as this has focus, the issue doesn't take place. So to quickly summarize this:
The issue doesn't take place when:
No other chrome windows (not selenium driven) are opened or minimised
Another chrome window (not selenium driven) is opened and has focus
Another application running fullscreen mode has focus
The issue does take place when:
Another chrome window (not selenium driven) is opened and has no focus
Note that when I talk about another chrome window I specificly mean a chrome window that is not driven by selenium, so a regular chrome window opened by the user.
Easiest way would be to run a local selenium grid. Start your node(s) as a windows service. This way the test will run in the background, without being headless.
Another good option to scale your solution, and if your machine has the capacity to run docker, is to use
zalenium
It's a docker based, auto scaling, selenium-grid solution that works pretty quick out of the box.
You can watch your tests live via the management pages, watch a recording after the fact, pause/debug with live interaction via VNC. Also something to be said for not having the worry about changing browser versions.
Last time i used it, it had the odd bug and throws an end of stream error every now and then - but that was a good year ago.

Selenium Webdriver c# when debugging if test fails Chromedriver does not close

I've run across an issue when running my Automation Tests. If i am debugging a test, it fails. If i after that stop the debugging, the browser is left open and the Chromedriver.exe task is left running in the background. its easy to close the chrome browser but the Chromedriver.exe is a bit more annoying as I could do a lot of debugging and have many of them left open. is there a correct way to close these in debugging and can I stop the test mid way? In my tests I use the
public void Dispose()
{
_driver.Quit();
}
and this works as long as I am not in debug mode and I stop the test in the middle. Any ideas on what should be the proper method to close these? Maybe a misunderstanding on my side to how im using Visual Studio. Any help is much appreciated.
Edit:
Im not sure thats related. When in debug mode, if you are running an automation test and if it fails. If you then stop the test at the fail point it leaves the browser open and the chromedriver running. my question is,
Is there a correct way to exit debug mode that closes the left open processes and browser. or is this a manual process or something that you use to do this outside of Visual Studio?
I do the same thing all the time. The problem is that you are stopping the test mid-run. When you do this, you leave the browser open. There are a couple options.
When you are done debugging, just hit Run. It will attempt to continue and fail and should close everything down properly.
Keep using it the way you currently do. You can kill processes using Task Manager, as needed. I have considered writing a batch file that kills all the chromedriver.exe processes, etc. You could do that and have it target IE and FF also to make cleaning up a lot of them easier.

Selenium c# : How to work with manually opened browser using selenium c#

I would like to debug test scripts when ever failed. I dont want to run the entire test script from startng onwards (launching browser..logging )
If my browser already opened (manually) and I would like to click on some objects (Web) , how can I achieve this programatically using selenium. I can do this in QTP but I dont know how can achieve this in selenium. Appreciate your quick help. !!
I am using Selenium 3.3 & IE 2.53 driver for Internet explorer
In general, the answer is that you can't do this. WebDriver explicitly does not support attaching to an existing browser instance that it did not start. This is a general principle, but is also explicitly true for Internet Explorer. I know that's not the answer you wanted to hear, but that's the way the IE driver is written at present.
I have faced similar problems. I work with an applications that has more than 10 screens.
We came up with a solution where we did not closed the browser if we encounter any failure. Have a solid exception handling in place and do not call Close() or Quit() methods to close the browser session.
Refer the below to come up with the program to continue automation with the already opened session. This is pretty much possible depending on how you designed your Automation framwork.
How to use a already opened firefox for testing in Selenium
Work on firefox for debugging purposes and then you can run the same test with Internet explorer
Note : I will update the answer with C# program to work with existing browser instances.

WaTiN issue - WaTin Freezes C# Application

I've just created a Winform in C# that uses WaTiN. The program works fine but whenever it opens new WaTiN browser windows in the background and navigates to a page etc. the application it freezes over and can't be used until WaTiN has finished its processes. Can you please help me to prevent this. Thankyou.
You should use background workers. The behaviour you're experiencing is absolutely normal. You can start reading about the workers here and here.

Killing Internet Explorer results in Crash Recovery prompt

I'm currently working on an application that uses an embedded internet explorer browser.
The application is written in C# in visual studio 2010. IE is IE9, but the problem also occurs in previous versions of IE.
The application is based on Oracle Customer On Demand, but is a standalone application that uses the embedded browser and adds some extensions to it.
Now the problem is, whenever I close my application, and open IE, IE tells me: -Your last browsing session closed unexpectedly. And it wants to reopen the page used in my application.
How can I prevent this? I know I can turn off "enable automatic crash recovery" but I don't think this is a clean solution. Is there a way to close IE from inside Visual studio properly?
At the moment it gets closed by doing:
foreach ( var ieProcessId in this.ieProcessIds )
{
Process.GetProcessById(ieProcessId).Close();
Process.GetProcessById(ieProcessId).Kill();
}
Kill() it's immediate stop (when application can't save own resoults).
Try more gently CloseMainWindow().

Categories