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();
Related
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.
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).
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.
I am using the following code (C#) based on the IWebBrowserApp com interface to find the Internet explorer window that matches the page I am trying to find, based on the title of the page.
I works fine if the page is on the first tab, but it does not work if its a later tab. So how do I get access to the tabs in internet explorer?
objSW = new ShellWindows();
IEnumerator ie = objSW.GetEnumerator();
while (ie.MoveNext())
{
obj = ie.Current;
app = (IWebBrowserApp)ie.Current;
System.Object docObj = app.Document;
HTMLDocumentClass hdoc = (HTMLDocumentClass)docObj;
if (hdoc.title.Contains(title)) matches.Add(app.HWND, app);
//do something
}
Sorry, but there's no supported API for tab enumeration/manipulation in IE9 or earlier.