I've been working on an app to crawl through a list of URLs and pull down the live source code (i.e. generated source instead of raw) - and I've used PhantomJS via Selenium.NET to do this as I wanted to have the operation done in a headless form.
The process itself works fine, but each time one of my threads loads an instance of the PhantomJS driver it runs PhantomJS.exe (as GhostDriver) in a newly opened console. I'd like to avoid this is possible as each time a thread starts the console windows are stealing focus on the machine. Does anyone know whether this is possible? I'm using a really basic snippet to launch the instance:
IWebDriver driver = new PhantomJSDriver();
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
driver.Navigate().GoToUrl(Entry);
///... Store HTML and do main work here ...
driver.Quit();
driver.Dispose();
Is there a way to hide the exe window via one of the additional capabilities that can be passed into the constructor, or a better way to load the instance via Process.Start etc..?
Any help would be appreciated as I can't seem to find any official documentation dealing with this issue for the .NET platform.
Thanks in advance!
The appearance of the console window is by design. The developer and maintainer of the .NET bindings believes strongly that launching processes that may output to the console while not displaying the console window is detrimental to debugging efforts. If this is really that huge an issue for you, you can launch PhantomJS.exe using your own System.Diagnostics.Process object, and use the RemoteWebDriver class to connect to and control it. Be aware, though, that you are then responsible for the lifetime of the PhantomJS.exe process, and will need to handle closing that process yourself (i.e., you can't rely on driver.Quit() to exit the process for you, like you can with the browser-specific PhantomJSDriver class).
Related
First I tried to add extension directly from chrome webstore, but problem is alert popup which appears after you click "add to chrome" is not reachable by selenium. After I wanted to try using options.AddExtension, but multilogin profile starts before selenium takes control so this method is useless. I know that I can manually import .crx for every profile but there are thousands of profiles and I need to automate process of installing extensions. I don't know what to do, I heared there's option to reach popup alert in chrome webstore but can't find it. Or maybe there are other ways to install extension after browser started? I'm glad for any help or advice
I have 2 possible scenarios for you, hope that works out since I don't have direct access to this specific need you have because the extensions may differ.
Not using Selenium, you gonna start the chrome process using the class Process and add the flag --load-extension= to load the specific profile/extension that you need. You can see the entire comma here. To sum, you can use this snippet below to load:
chrome --user-data-dir=/tmp/someuniquedirname --load-extension=path/to/extension --no-first-run //Note: some flags may change between versions of chrome, see full documentation
After defining the extension and start chrome, you can now get hold of the process with Selenium by using another flag: --remote-debugging-port=http://localhost:[localporthere]. After that, start the process than tell to Selenium to get hold of that process with the port and do your job.
Another way is to start the process installing the extension manually and in another Thread use some Automation UI (Teststack.White or FlaUI) to click on the popup you have. I can't extend here in the entire code for this solution because it will go to a opinion-based answer, but you can check on FlaUI for that use, and follow that path:
Selenium starts the program, click on the install extension and wait for the popup to show-up;
New Thread;
FlaUI gets hold of the process that you already start;
Using UIElements, click on the "ok" button you need;
FlaUI drops;
Back to the main thread.
If you need any clarifications about the solutions above, just comment and I'll try to help you further.
I want to make a GUI Windows application that can run console applications. When this happens, the console window should not be shown. Instead its content should be shown in a visual component (memo/richedit). This component should show exactly the same content which would appear in the console window, even the color of the text and its background should be displayed. So this visual component should work exactly as a console window. I know that the standard output can be captured but many console applications do not use it. Is it possible to capture the output of a console application this way? Are there Windows API calls that can handle this?
I use Delphi XE2 but C# code would also be helpful.
You have to run the console mode program with stdout redirection to a pipe that your Delphi program will create. Your Delphi program can read the pipe to get the console mode program output and do whatever it needs to. By the way, this works not only with Delphi but also with any language able to create pipe and run program with I/O redirection.
If you need Delphi code to do that, have a look at this reference.
There is a ready-to-run component on GitHub: DosCommand
The Demo shows two ways how to do what you describe.
I am not sure if it works for older versions like XE2, but at least you can give it a try.
Traditionally you would call CreateProcess with stdin/stdout set to pipes you created. This should work for most programs but not for anything that uses a ncurses style "GUI" and you also lose the color information. An example can be found on MSDN.
Windows 10 (1809?) added support for pseudoconsoles. This is used by the new Terminal application and is your best bet for full console compatibility.
The last alternative is to inject into the child process and hook WriteFile, ReadFile and all the console functions but this is ugly and error-prone.
Is there a way to monitor processes starting in the system before they start?
Example:
On programs like ZoneAlarm or Antivirus programs, when you run a program it asks you if you allow running this program or not before it runs...
There's a few ways to do this. If you only need to track process creation coming from a specific program (or a few programs), the EasyHook/Detours method mentioned here will work pretty well, but you effectively need to install a hook on CreateProcess into each program, so it's not a great solution if you want to track all process creation in the system.
There's a specific API for this in NT-based Windows variants (NT/2000/XP/Vista) called PsSetCreateProcessNotifyRoutine(). Unfortunately, you can only call this function from ring0, so it needs to be done in a driver. There's a handy explanation (and code) in this CodeProject article: http://www.codeproject.com/KB/threads/procmon.aspx.
AFAIK, this is just a notification, and does not by itself allow you to tell the system whether the process should be created or not. However, if you needed to do this, you could pause the process (e.g. by attaching to it as a debugger) while your code decides whether to kill it or not.
You should check out the easyhook-continuing-detours project, which is a .NET port of the Microsoft Detours project. It will allow you to hook unmanaged APIs (such as CreateProcess). Check out code examples for a simple FileMon-like program here.
You can find out when processes start via using a real-time ETW consumer - however, to be able to take some action that could possibly cancel the process from starting, you'll have to do something shady / undocumented, like hooking CreateProcess, or using a kernel filter driver to block reads to the EXE.
Just use process creation notifications .
It's included in Windows.
You don't need to hook anything.
When I start
/Applications/Firefox.app/Contents/MacOS/firefox-bin
on MacOSX using Process.Start() using Mono, the id of the process that gets returned does not match the process that firefox ends up running under.
It looks like firefox quickly decides to start another process, and kill the current one.
This makes it difficult to stop firefox, and to detect if it is still running. I've tried starting firefox using the -no-remote flag, to no avail.
Is there a way to start firefox in such a way that it doesn't do this "I'll quickly make a new process for you" dance?
The situation can somewhat be detected by making sure Firefox keeps on running for at least 3 seconds after its start, and when it does not, scan for other firefox processes. However, this technique is shaky at best, as on slow days it might take a bit more than 3 seconds, and then all tests depending on this behaviour fail.
It turns out, that this behaviour only happens when asking firefox to start a specific profile using -P MyProfile. (Which I need to do, as I need to start firefox with specific proxyserver settings) If I start firefox "normally" it does stick to its process.
When using mozilla programmatically / embedding in other apps you should use the provided components:
"The right way" as recommended by mozilla ...
https://developer.mozilla.org/en/XPCOM
"The easy way", general consensus from Mozilla dev guys ...
http://www.iol.ie/~locka/mozilla/control.htm
Need to "execute processes", simply instantiate an instance of the component and talk to that, you should then have everything you need.
However ...
If you really do insist on looking after the actual process for a firefox instance, you may want to call
Process.Start("...\firefox.exe");
Then try this (i believe this works on MAC OS too) ...
C#/mono: get list of child processes on Windows and Linux
The basic idea / thought pattern being that if it is in fact executing another process right away you should be able to find that as would be a "child" process instead of being just one from a list in the "task list" / process manager for the machine.
In case you are ok with the detection of Firefox process itself but don't know when you should start the detection, you can use the Process.Exited event. Don't forget to set EnableRaisingEvents first.
I suggest taking a different approach: list all running processes, find Firefox, get it's process ID and (optional) see if it is the one you started.
I'm not sure if there's a platform-independent way to accomplish this. On Linux, you can call "ps -ef" to list them, on Windows it's called "tasklist".
I don't have a Mac, so I can't be sure, but if it's anything like Linux the firefox 'binary' is actually a shell script which sets up a bunch of required environment variables and checks if firefox is already running (if it is, it will try and add a new tab to the existing instance, as far as I know).
You may be able to capture the new PID within the script, although if you update it do beware of it being overwritten during upgrades.
Is there a way to monitor processes starting in the system before they start?
Example:
On programs like ZoneAlarm or Antivirus programs, when you run a program it asks you if you allow running this program or not before it runs...
There's a few ways to do this. If you only need to track process creation coming from a specific program (or a few programs), the EasyHook/Detours method mentioned here will work pretty well, but you effectively need to install a hook on CreateProcess into each program, so it's not a great solution if you want to track all process creation in the system.
There's a specific API for this in NT-based Windows variants (NT/2000/XP/Vista) called PsSetCreateProcessNotifyRoutine(). Unfortunately, you can only call this function from ring0, so it needs to be done in a driver. There's a handy explanation (and code) in this CodeProject article: http://www.codeproject.com/KB/threads/procmon.aspx.
AFAIK, this is just a notification, and does not by itself allow you to tell the system whether the process should be created or not. However, if you needed to do this, you could pause the process (e.g. by attaching to it as a debugger) while your code decides whether to kill it or not.
You should check out the easyhook-continuing-detours project, which is a .NET port of the Microsoft Detours project. It will allow you to hook unmanaged APIs (such as CreateProcess). Check out code examples for a simple FileMon-like program here.
You can find out when processes start via using a real-time ETW consumer - however, to be able to take some action that could possibly cancel the process from starting, you'll have to do something shady / undocumented, like hooking CreateProcess, or using a kernel filter driver to block reads to the EXE.
Just use process creation notifications .
It's included in Windows.
You don't need to hook anything.