c# : Close chromium browsers without close google chrome browsers - c#

I would like to close chromium web processs
without closing google chrome browser which are running
The code bellow close chromium browsers but also google chrome browsers, that I don't want to :
var chromeAndChomiumProcesses = Process.GetProcessesByName("chrome");
foreach (var chromeAndChomiumProcess in chromeAndChomiumProcesses)
{
chromeAndChomiumProcess.Kill();
}
Do you know how to do this?

This may work if you know the path to Chromium. Plus, You will have to compile the code as x64.
Process[] chrome = Process.GetProcessesByName("chrome");
foreach (var chromeProcess in chrome)
{
string fullPath = chromeProcess.MainModule.FileName;
string expectedPath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
if (fullPath.Equals(expectedPath))
{
chromeProcess.Kill();
}
}
Also keep in mind this comparison needs to be case sensitive.

Related

C# Selenium Edge Driver unable to download file - Keep file prompt shows up

I am using C# with Selenium for QA automation, and I am having issues with downloading an .xml file, because a prompt is always showing up asking if I want to keep the file. It also opens a second tab to execute the download, closing it after the prompt shows up.
[keep file prompt][1]
Using Chrome I do not see this behavior.
I searched all over and could not find a EdgeOptions() and/or AddArguments() capable of taking care of this issue.
Any ideas?
You need to use JS to interact with elements in another browser. I have had such experience and I used if else statement in my method to handle that problem. Just look trough the Selenium documentation, JS with selenium examples and so long so for.
Just add this to your OneTimeSetup method. Make sure to run Visual Studio as administrator. This works since Edge 105+:
public void SetEdgeXmlDownloadPolicy()
{
var keyName = "Software\\Policies\\Microsoft\\Edge\\";
var valueName = "ExemptFileTypeDownloadWarnings";
var valueData = #"{""domains"": ["" * ""], ""file_extension"": ""xml""}";
var currentUser = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
var currentKey = currentUser.OpenSubKey(keyName, true);
if (currentKey == null)
currentKey = currentUser.CreateSubKey(keyName);
if (currentKey.GetValue(valueName) == null)
currentKey.SetValue(valueName, valueData);
}

C# get URLs of all open pages in Firefox

I have already been here:
C# get URL from firefox but don't use DDE
How can I get URLs of open pages from Chrome and Firefox?
C# - Get all open browsing tabs in all instances of firefox
https://social.msdn.microsoft.com/Forums/vstudio/en-US/93001bf5-440b-4a3a-ad6c-478a4f618e32/how-can-i-get-urls-of-open-pages-from-chrome-and-firefox?forum=csharpgeneral
Get Firefox URL?
I know there are lots of questions already out there about this topic, BUT none of them answers it correctly. I am curious how to get the URL of all open pages in firefox, but i dont find a solution that provides working code.
This is the most rewarded solution on the internet, but it does not work for me.
This code uses DDE (which used NDDE - a good DDE wrapper for .NET):
private string GetBrowserURL(string browser)
{
try
{
DdeClient dde = new DdeClient(browser, "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
dde.Disconnect();
return text[0].Substring(1);
}
catch
{
return null;
}
}
I dont care about if it shows the history, gets the URLs of open pages in a second Firefox window, i want to keep it simple by now. Please dont provide me code for another browser as it is always browser specific or any VB code.
This works even if you use Firefox > 49:
System.Windows.Automation.AutomationElement AutomationElement = System.Windows.Automation.AutomationElement.FromHandle(ptr);
System.Windows.Automation.AutomationElement Elm = AutomationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
System.Windows.Automation.AutomationPattern[] BAutomationPattern = Elm.GetSupportedPatterns();
System.Windows.Automation.ValuePattern BValuePattern = (System.Windows.Automation.ValuePattern)Elm.GetCurrentPattern(BAutomationPattern[0]);
CurrentUrlName = BValuePattern.Current.Value.ToString();

Open a webpage in new tab on IE, even if it's not the default browser

Background
We are developing an application where you can search some stuff on the internet or open a webpage and you can chose what browser to use as well.
So if I would like to open Google.com for example, and I want it to open in Chrome, then the webpage should open in Chrome. In the event that I want to open Google.com in IE, then IE should open the Google page.
Now about using a tab in the browser: since all browsers now support it, opening a webpage in a new tab is already taken care of by the browser itself, whether it be Chrome or Firefox. But in the case of IE, if IE is your default browser, then IE will open the webpage in a new IE tab. However, if IE is not your default browser, then IE will instead open the web page in a new IE window.
Some additional information
There are several ways of opening a web page
via:
code for your default web browser
Process.Start(new ProcessStartInfo()
{
FileName = "http://www.google.com"
});
or if you want to open the webpage in another webbrowser besides the default one. Firefox for example
string a = "%programfiles%\\Mozilla Firefox\firefox.exe";
a = Environment.ExpandEnvironmentVariables(a);
Process.Start(new ProcessStartInfo()
{
FileName = a,
Arguments = "http:\\www.google.com"
});
command
>start "http://www.google.com"
or
cmd /c start "http://www.gooogle.com"
Question
How do you open a webpage in IE(for version 8, 9, and 10) on a new tab even if IE is not your default browser?
Windows understands shorthand for any entries in the registry here:
HKEY_LOCAL_MACHINE
SOFTWARE
Microsoft
Windows
CurrentVersion
App Paths
So, assuming the browser installs happened without issues, each client will have entries such as:
Firefox.exe
IEXPLORE.exe
Chrome.exe
Which means, you can actually use this sort of thing:
Process.Start(new ProcessStartInfo()
{
FileName = "firefox.exe",
Arguments = " \"http://www.google.com\""
});
Process.Start(new ProcessStartInfo()
{
FileName = "iexplore.exe",
Arguments = " \"http://www.google.com\""
});
Process.Start(new ProcessStartInfo()
{
FileName = "chrome.exe",
Arguments = " \"http://www.google.com\""
});
.. thereby targeting a specific browser.
If it is intended that you are fixed on using Internet Explorer for this, this is what you can do:
Create a (temporary) script file called temp.js. Put this in it:
var navOpenInBackgroundTab = 0x1000;
var objIE = new ActiveXObject("InternetExplorer.Application");
objIE.Navigate2(FIRST TAB URL GOES HERE);
objIE.Navigate2(SECOND TAB URL GOES HERE, navOpenInBackgroundTab);
objIE.Navigate2(NTH TAB URL GOES HERE, navOpenInBackgroundTab);
objIE.Visible = true;
Then you call this script in the directory you created it: wscript temp.js
Don't forget to delete it afterwards:
Oh, and if this sounds like a terrible hack, trust me: it is.

Same process IDs of all IE windows

When I manually launch Internet Explorer windows, each one of them gets a different PID.
However, suppose I have manually opened one browser window and it gets PID 12345 and then run WaitiN which opens some browser windows, the PIDs of all WaiiN windows becomes 12345. How can I change the behaviour so that WatiN works like it works manually.
I need the PIDs so that I can close the broswer windows opened by WatiN in my cleanup code and make sure I close the PIDs of browser windows opened only by WatiN.
EDIT:
Code I am using is:
//Where I am using WatiN 1
IE ReportWindow = IE.AttachToNoWait<IE>(Find.ByTitle("New Activity Recap"), 120);
ReportWindowPID = ReportWindow.ProcessID;
//Where I am using WatiN 2
IE ReportWindow = IE.AttachToNoWait<IE>(Find.ByTitle("New Activity Recap"), 120);
ReportWindowPID = ReportWindow.ProcessID;
//Cleanup code
foreach (Process p in System.Diagnostics.Process.GetProcessesByName("iexplore"))
{
if (p.Id == ApplicationWindowPID || p.Id == ReportWindowPID)
{
try
{
p.Kill();
p.WaitForExit();
}
catch
{
}
}
}
The above code is closing the 2 windows opened by WatiN as well as other windows.
PS: ANy code which kills the browser windows by a URL can also do because the base URL is going to be the same in the 2 WatiN windows.

watin attach to manually started window keeps failing

IList<string> values = new List<string>();
var instance = Find.By("hwnd", "110CC");
...
if(instance != null)
{
var ie = Browser.AttachTo<IE>(instance);
The browser instance is manually started by the tester in case this makes any difference.
This just doesn't work for me I keep getting an exception from watin saying that it can't find a window with that handle.
I got the handle with Spy++.
I tried searching by window title or window url also but it also didn't work.
Is there any way to do this?
Thank you
The below works as expected / no errors. WatiN 2.1, IE9, Win7
Before running the code, open an IE browser and point it at cnn.com
IE browser = Browser.AttachTo<IE>(Find.ByUrl("www.cnn.com"));
browser.TextField("hdr-search-box").TypeText("searchy");

Categories