TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Smile
1.5k
202
37k
How to all the urls of the open tabs of a browser
Feb 7 2020 6:48 AM
I am trying to read all the urls of all the tabs of a browser i.e. google chrome.By the below code i am able to get only the active tab url.But i want all the urls of the tabs of a browser.
below is the code
System.Diagnostics.Process[] procsChrome = System.Diagnostics.Process.GetProcessesByName(
"CHROME"
);
foreach
(Process proc
in
procsChrome)
{
string
procname = proc.ProcessName;
// the chrome process must have a window
if
(proc.MainWindowHandle == IntPtr.Zero)
continue
;
// to find the tabs we first need to locate something reliable - the 'New Tab' button
AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
var SearchBar = root.FindFirst(TreeScope.Descendants,
new
PropertyCondition(AutomationElement.NameProperty,
"Address and search bar"
));
if
(SearchBar !=
null
)
{
string
str = (
string
)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);
}
}
Even i tried below one also
private
string
GetUrl(BrowserType browser)
{
if
(browser == BrowserType.Chrome)
{
//"Chrome_WidgetWin_1"
Process[] procsChrome = Process.GetProcessesByName(
"chrome"
);
foreach
(Process chrome
in
procsChrome)
{
// the chrome process must have a window
if
(chrome.MainWindowHandle == IntPtr.Zero)
{
continue
;
}
//AutomationElement elm = AutomationElement.RootElement.FindFirst(TreeScope.Children,
// new PropertyCondition(AutomationElement.ClassNameProperty, "Chrome_WidgetWin_1"));
// find the automation element
AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
// manually walk through the tree, searching using TreeScope.Descendants is too slow (even if it's more reliable)
AutomationElement elmUrlBar =
null
;
try
{
// walking path found using inspect.exe (Windows SDK) for Chrome 29.0.1547.76 m (currently the latest stable)
var elm1 = elm.FindFirst(TreeScope.Children,
new
PropertyCondition(AutomationElement.NameProperty,
"Google Chrome"
));
var elm2 = TreeWalker.ControlViewWalker.GetLastChild(elm1);
// I don't know a Condition for this for finding :(
var elm3 = elm2.FindFirst(TreeScope.Children,
new
PropertyCondition(AutomationElement.NameProperty,
""
));
var elm4 = elm3.FindFirst(TreeScope.Children,
new
PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));
elmUrlBar = elm1.FindFirst(TreeScope.Children,
new
PropertyCondition(AutomationElement.NameProperty,
"Address and search bar"
));
}
catch
(Exception ex)
{
ex.ToString();
// Chrome has probably changed something, and above walking needs to be modified. :(
// put an assertion here or something to make sure you don't miss it
continue
;
}
// make sure it's valid
if
(elmUrlBar ==
null
)
{
// it's not..
continue
;
}
// elmUrlBar is now the URL bar element. we have to make sure that it's out of keyboard focus if we want to get a valid URL
if
((
bool
)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))
{
continue
;
}
// there might not be a valid pattern to use, so we have to make sure we have one
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if
(patterns.Length == 1)
{
string
ret =
""
;
try
{
ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;
}
catch
{ }
if
(ret !=
""
)
{
// must match a domain name (and possibly "https://" in front)
if
(Regex.IsMatch(ret, @
"^(https:\/\/)?[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,4}).*$"
))
{
// prepend http:// to the url, because Chrome hides it if it's not SSL
if
(!ret.StartsWith(
"http"
))
{
ret =
"http://"
+ ret;
}
return
ret;
}
}
continue
;
}
}
}
return
null
;
}
The line high lighted is throwing exeception object refernce is null
Reply
Answers (
1
)
Running Total in C#
get TOTAL of all textbox enteries in a single textbox