Start Opera and wait while it is closed
I want to start the Opera web browser as a process of visiting a specific website and then wait until it is closed.
A special situation is that the browser may already be open and running because the user may have already visited some website.
In this case, the browser will likely open a new tab in the existing window and the recently launched process will be terminated immediately. This should not confuse my waiting process. I either want a new browser window (if it can be done somehow, maybe with command line arguments) and wait for this to close, or save the existing browser window and wait for all tabs resulting from my process to close ...
I think it doesn't matter, but my programming environment
and you can assume that I know the path to the browser.
The only browser I can get the expected behavior for is Internet Explorer (sigh). Here I need to create a new batch version of the script in a temporary folder with something like
start /WAIT "" "C:\Program Files\Internet Explorer\iexplore.exe" -noframemerging http://www.test.com/
Then I run the batch script instead of the direct browser and delete it as soon as I am done waiting.
To make the required process clearer:
- My program will start.
- My program starts the Opera browser as a separate process and provides the visit URL as an argument to this process.
- Opera browser runs asynchronously like a new process and visits the provided URL. It's easy so far.
- After starting a new process (Opera browser), my own program has to wait for this process to complete. This is the hard part because
- Many modern browsers run multiple processes. I will need to wait for them all.
- Many modern browsers can somehow "detach" from the process I was running. Sorry, I don't know a better word, I mean: I start a process, which then starts another process and terminates immediately while the other process continues to run. If I expect the browser process originally started by my program, the wait will complete while the browser is still open.
- A special case of the above is the tabbed browsing implemented in many browsers: if the browser is already open (in a separate process started by the user) when it starts, my newly launched browser can simply pass the URL to visit the existing process and terminate. The user is still at my provided URL while my program thinks it has closed the browser. This problem can be prevented by specifying a special command line argument, for example
- Once the browser exits, or all the tabs associated with the URL I specified have been closed, my program will stop waiting and continue its activity instead.
The use case is that I have a web application that can run locally or on a server. If it runs locally, it starts a web server and then opens a browser to visit the login page. When the browser is closed, this web application must also be disabled. It works safely for Internet Explorer. For all other cases, the user must close the browser and then, explicitly, the web application. Thus, if I could reliably wait for Opera to finish, it would greatly improve the user experience.
The solutions are preferred in the following order
- Everything comes with a pure Java JRE. This includes browser-specific command line arguments.
- Things that require me to, for example, create a batch script (for example in the case of IE).
- Anything that requires third party open source libraries.
- Anything that requires a 3rd party closed source library.
Any programming language independent solution (like command line arguments only) is preferred over pure Java. Any platform independent answer (working on both Windows and Linux) is preferred over platform specific.
Cause. Ideally, I would like to know what exactly is being done and include it in my own code. Since I want to support different browsers (see "PS" below), I would like to not include one library per browser. Finally, I cannot use commercial or proprietary libraries, but unless a better answer comes up, I will of course mark any working solution with acceptance. I'll accept the first (reasonably good) working answer like "1". If lower preference answers come up, I will wait a few days before accepting the better one.
I ran a couple of similar questions for other browsers. Since I believe browsers are very different in the command line arguments they digest and how they start threads and subprocesses, I think this makes sense. If I were just asking how to start / any browser and wait for it to finish, it would be quite difficult to figure out the "correct" answer. For example, a single question in a famous browser might be a good starting point.
- Similar question regarding Firefox: Start Firefox and wait until it is closed
- Similar question regarding Chrome: Start Chrome and wait until it is closed
- A similar question regarding Chromium: Start Chromium and wait until it is closed
- Similar question regarding Edge: Launch Edge Browser and wait until it is closed
- Similar question regarding Safari: Launch Safari and wait until it is closed
source to share
No one has answered this question yet
See similar questions: